我需要做一个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))
);
Run Code Online (Sandbox Code Playgroud)
我在标题上尝试了类似的东西($itemJson
是一个JSON字符串),但我得到错误400.
我想我是以错误的方式做这个请求的.有谁能够帮我?
GBD*_*GBD 10
你可以尝试如下
$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);
Run Code Online (Sandbox Code Playgroud)