PHP POST数据至远程服务器获取信息

当一个跨域远程页面需要通过$_POST获取参数时,你是使用什么方式传递这些参数并获取响应数据呢?下面为您提供一种PHP Post()方法。

View Code
 1 <?php
 2 header("Content-type: text/html; charset=utf-8"); 
 3 function Post($url, $post = null) {
 4     $context = array();
 5     if (is_array($post)) {
 6         ksort($post);
 7         $context['http'] = array (
 8             'timeout'=>60,
 9             'method' => 'POST',
10             'content' => http_build_query($post)
11         );
12     }
13     return file_get_contents($url, false, stream_context_create($context));
14 }
15 
16 $data = array (
17     'type' => 'text',
18     'inputValue' => '哈哈'
19 );
20 $result = Post('http://tool.anzhuoxiazai.com:80//servlet/QRServlet', $data);
21 echo str_replace("src='","src='http://tool.anzhuoxiazai.com/",$result);
22 //End_php

 参考文章:file_get_contents 增加超时的时间限制 

注:cURL系列提供了更加完备的功能支持,请参考《PHP cURL 应用

 1 function Post($url, $post = null) {
 2     $context = array();
 3     if (is_array($post)) {
 4         ksort($post);
 5         $context['http'] = array (
 6             'timeout'=>60,
 7             'method' => 'POST',
 8             'content' => http_build_query($post)
 9         );
10     }
11     return file_get_contents($url, false, stream_context_create($context));
12 }
作者:Zjmainstay
         
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息
原文地址:https://www.cnblogs.com/Zjmainstay/p/php_post_get_data.html