[英]curl -X POST -H 'Content-Type: application/json' -d to PHP
I need to make a curl request, I have this line "curl -X POST -H 'Content-Type: application/json' -d" and need to "translate" to PHP curl. 我需要做一个curl请求,我有这行“curl -X POST -H'Content-Type:application / json'-d”并且需要“翻译”到PHP curl。 The problem is I don't know what the "-X", "-H" and "-d" mean.
问题是我不知道“-X”,“ - H”和“-d”是什么意思。
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: '. strlen($itemJson))
);
I tried something like that on header ( $itemJson
is a JSON string) but I got error 400. 我在标题上尝试了类似的东西(
$itemJson
是一个JSON字符串),但我收到错误400。
I think I'm doing the request in a wrong way. 我想我是以错误的方式做这个请求的。 Can anybody help me?
有谁能够帮我?
You can try as below 你可以尝试如下
$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.