简体   繁体   English

如何使用php_curl发送json数据

[英]How to send json data using php_curl

我正在尝试使用 php_cURL 将 JSON 数据发送到 API,但没有成功。如果有人使用相同的方法,请共享代码。

Try this:尝试这个:

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

Source : http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl来源: http : //www.lornajane.net/posts/2011/posting-json-data-with-php-curl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM