|
[U]复制代码[/U] 代码如下:
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception(" roblem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception(" roblem reading data from $url, $php_errormsg");
}
return $response;
}
用法如下:
[U]复制代码[/U] 代码如下:
//json字符串
$data = "{...}";
//转换成数组
$data=json_decode($data,true);
$postdata = http_build_query($data);
do_post_request("http://localhost",$postdata);
您可能感兴趣的文章:php获取通过http协议post提交过来xml数据及解析xmlphp中用socket模拟http中post或者get提交数据的示例代码php curl模拟post提交数据示例php curl post 时出现的问题解决php发送post请求的三种方法php过滤所有恶意字符(批量过滤post,get敏感数据)PHP函数分享之curl方式取得数据、模拟登陆、POST数据PHP防止post重复提交数据的简单例子php发送get、post请求的6种方法简明总结php以post形式发送xml的方法
|
|