简体   繁体   English

Php CURL虽然我将标题设置为application / json,但请求仍然是application / x-www-form-urlencoded

[英]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.

相关问题 使用标题应用程序/ x-www-form-urlencoded进行卷曲发布 - curl posting with header application/x-www-form-urlencoded 如何使用内容类型为“ application / x-www-form-urlencoded”的PHP curl发送原始JSON? - How can I send a raw JSON using PHP curl with the content type “application/x-www-form-urlencoded”? ionic和php之间的application / x-www-form-urlencoded - application/x-www-form-urlencoded between ionic and php php-使用php处理应用程序/ x-www-form-urlencoded POST请求 - php - Handle application/x-www-form-urlencoded POST request using php 如何在基于XML的API调用中将标头设置为“ application / x-www-form-urlencoded” - How do I set the headers to “application/x-www-form-urlencoded” on a XML based API Call 为什么Zend Framework 2会使用application / x-www-form-urlencoded进行放置请求? - Why does Zend Framework 2 use application/x-www-form-urlencoded for put request? 使用Content-Type = application / x-www-form-urlencoded发送POST请求时,POST值为空 - POST value is empty when Sending POST request with Content-Type = application/x-www-form-urlencoded 接受在Codeigniter中的application / x-www-form-urlencode中传递的请求数据 - Accepting Request data passed in application/x-www-form-urlencoded in Codeigniter 的file_get_contents( &#39;PHP://输入&#39;); 与application / x-www-form-urlencoded; - file_get_contents('php://input'); with application/x-www-form-urlencoded; PHP应用程序/ x-www-form-urlencoded错误地解码了特殊字符 - PHP application/x-www-form-urlencoded decodes special character wrongly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM