[英]PHP curl changes the Content-Type to application/x-www-form-urlencoded. Shouldn't do that
[英]curl -X POST -H 'Content-Type: application/json' -d to PHP
我需要做一个curl请求,我有这行“curl -X POST -H'Content-Type:application / json'-d”并且需要“翻译”到PHP curl。 问题是我不知道“-X”,“ - H”和“-d”是什么意思。
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: '. strlen($itemJson))
);
我在标题上尝试了类似的东西( $itemJson
是一个JSON字符串),但我收到错误400。
我想我是以错误的方式做这个请求的。 有谁能够帮我?
你可以尝试如下
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://somedomain.com/test.php');
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.