利用php curl发送json数据与curl post其它数据是一样的,下面我来给大家总结几个关于curl post发送json数据实例,希望能加深各位对curl post json数据的理解吧。
例1代码如下:
"Hagrid",
"age" => "36"
);
$data_string = json_encode($data);
$ch = curl_init('http://api.local/rest/users');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
));
$result = curl_exec($ch);
?>例2,代码如下:
1,
'b' => 2
));
list($return_code, $return_content) = http_post_data($url, $data);
?>例3代码如下:
小结,我们发现最核心的一句代码就是Content-Type: application/json;这个是文件格式类型了.
立即学习“PHP免费学习笔记(深入)”;











