Home > Article > Backend Development > PHP sends json data instance through curl post_PHP tutorial
Using php curl to send json data is the same as curl post other data. Let me summarize a few examples of curl post sending json data. I hope it can deepen your understanding of curl post json data.
Example 1
代码如下 | 复制代码 |
$data = array("name" => "Hagrid", "age" => "36"); |
例2
代码如下
|
复制代码 | ||||||||
function http_post_data($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
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://xx.xx.cn";
$data = json_encode(array('a'=>1, 'b'=>2));
list($return_code, $return_content) = http_post_data($url, $data);
例3
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//$data JSON类型字符串 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data))); $data = curl_exec($ch); print_r($data);//创建成功返回:{"errcode":0,"errmsg":"ok"} true http://www.bkjia.com/PHPjc/633132.html TechArticle利用php curl发送json数据与curl post其它数据是一样的,下面我来给大家总结几个关于curl post发送json数据实例,希望能加深各位对curl post json数...
|