当前位置:首页 > PHP教程 > PHP高级教程

解析PHP 使用curl提交json格式数据

复制代码 代码如下:

$data = array("name" => "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);



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!