Php CURL虽然我将头设置为application/json,但请求将以application/x-www-form


Php CURL Though I set the header as application/json the request goes as application/x-www-form-urlencoded

我正在尝试在代码中POST JSON
虽然我设置了头Content_Type:application/json
,但接收服务器将成为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);

但是接收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 > 

CURLOPT_HTTPHEADER是一个数组,其中包含一个单独的项,即字符串或逗号分隔的标头。

您应该将其设置为关联数组,其中每个键/值对都是一个标头。

您还需要正确拼写标题名称。内容类型的中间有一个连字符,而不是下划线。

$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);