欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-13 08:52:10
复制代码 代码如下:$data = array("name" => "hagrid", "age" => "36");$data_string = json_e...

复制代码 代码如下:

$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);