function do_post_request($url, $data, $optional_headers = null)<BR>{<BR> $params = <i>1本文来#源gaodai$ma#com搞$代*码*网</i><pre>搞代gaodaima码
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(“Problem with $url, $php_errormsg”);
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception(“Problem reading data from $url, $php_errormsg”);
}
return $response;
}
用法如下:
//json字符串<BR>$data = "{...}";<BR>//转换成数组<BR>$data=json_decode($data,true);<BR>$postdata = http_build_query($data);<BR>do_post_request("http://localhost",$postdata);<BR>