|
復(fù)制代碼 代碼如下:
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("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;
}
用法如下:
復(fù)制代碼 代碼如下:
//json字符串
$data = "{...}";
//轉(zhuǎn)換成數(shù)組
$data=json_decode($data,true);
$postdata = http_build_query($data);
do_post_request("http://localhost",$postdata);
php技術(shù):php發(fā)送post請(qǐng)求函數(shù)分享,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。