[英]Php CURL Though I set the header as application/json the request goes as application/x-www-form-urlencoded
I am trying to POST JSON in my code. 我想在我的代码中POST JSON。
Though I set the header Content_Type:application/json
虽然我设置了标题
Content_Type:application/json
the receiving server gets as Content-Type: application/x-www-form-urlencoded
接收服务器获取为
Content-Type: application/x-www-form-urlencoded
$Content_Type = "application/json";
$header_info = "X_RESTBUS_MESSAGE_ID:".$X_RESTBUS_MESSAGE_ID.","."X_BU_ID:".$X_BU_ID.","."Content_Type:".$Content_Type;
$url = $server_url;
$content = json_encode($body_data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array($header_info));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
but the server receiving the POST call gets as 但接收POST呼叫的服务器获取为
13 > X_RESTBUS_MESSAGE_ID: <aaaaa>,X_BU_ID:<xxxx>,Content_Type:application/json
13 > Accept: */*
13 > Content-Type: application/x-www-form-urlencoded
13 > Content-Length: 641
13 > Host: localhost:49111
13 >
You've CURLOPT_HTTPHEADER
to be an array with a single item in it, which is a string or comma separated headers. 你有一个
CURLOPT_HTTPHEADER
是一个包含单个项目的数组,它是一个字符串或逗号分隔的标题。
You should set it to be an associative array where each key/value pair is a single header. 您应该将其设置为一个关联数组,其中每个键/值对都是单个标头。
You also need to spell the header name correctly. 您还需要正确拼写标题名称。 Content-Type has a hyphen in the middle, not an underscore.
Content-Type在中间带有连字符,而不是下划线。
$headers = array(
"X_RESTBUS_MESSAGE_ID" => $X_RESTBUS_MESSAGE_ID,
"X_BU_ID" => $X_BU_ID,
"Content-Type" => $Content_Type
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.