CURL modifying request headers

I'm hoping someone can help me. I'm sending an API request, via CURL, using PHP. The headers to the third party application need to be as follows:

    $headers = array( 
        "content-type: application/json",
        "Accept: application/json"
    );

The CURL request is initialised and sent as follows:

    // 1. initialize
    $ch = curl_init();

    // 2. set the options, including the url
    curl_setopt($ch, CURLOPT_URL, $this->sendPropertyUrl);
    curl_setopt($ch, CURLOPT_HEADER, $headers);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSLCERT, $this->certPath);
    curl_setopt($ch, CURLOPT_CAINFO, $this->certPath);
    curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $this->certPassword);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $propertyDataString);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // 3. execute and fetch the resulting HTML output
    $rightmoveResponse = curl_exec($ch);
    $output = json_decode($rightmoveResponse, true);

However if I trap the header info actually sent in the CURL request the output is as follows:

    POST /v1/property/sendpropertydetails HTTP/1.1
    Host: adfapi.adftest.rightmove.com
    Accept: */*
    Content-Length: 1351
    Content-Type: application/x-www-form-urlencoded
    Expect: 100-continue

Can anyone explain why CURL has modified the Accept and Content-Type parameters?

Any help greatly appreciated.

Brian


What you want is defined by CURLOPT_HTTPHEADER instead of CURLOPT_HEADER .

From PHP Docs:

CURLOPT_HEADER TRUE to include the header in the output.

CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the format array ('Content-type: text/plain', 'Content-length: 100')

See this user note about using it.


You used curl_setopt($ch, CURLOPT_POST, true); which sets the content-type. Instead use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); (see http://php.net/manual/en/function.curl-setopt.php).

This is in addition to what @Alan Machado said.

链接地址: http://www.djcxy.com/p/48908.html

上一篇: 如何在java中创建一个自定义@jsonDeserializer类

下一篇: CURL修改请求标头