PHP通过http post发送json数据

2017-01-06 来源:apizl.com 点击:181
function http_post_data($url, $data) {
 $data=json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data))
);
ob_start();
curl_exec($ch);
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($return_code, $return_content);
}
$url  = "http://www.test.com";
var_dump(http_post_data($url.['a'=>1]));

QQ截图20170106090556.png

文章由爱资料原创 来源:http://www.apizl.com/archives/view-132991-1.html,欢迎分享本文,转载请保留出处!
Tag:php  php常见  
分享: