如何使用php_curl发送json数据
我正在尝试使用php_cURL将JSON数据发送到API,但没有成功。如果有人使用相同的方法,请共享代码。尝试以下方法:如何使用php_curl发送json数据,php,curl,Php,Curl,我正在尝试使用php_cURL将JSON数据发送到API,但没有成功。如果有人使用相同的方法,请共享代码。尝试以下方法: $data = array("name" => "Hagrid", "age" => "36"); $data_string = json_encode($data);
$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);
来源:谢谢分享。我使用的是相同的,但是我的API运行在HTTPS上