CURL修改请求标头

时间:2015-09-18 16:41:11

标签: php curl

我希望有人可以帮助我。我正在通过CURL使用PHP发送API请求。第三方应用程序的标头需要如下:

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

CURL请求已初始化并按如下方式发送:

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

但是,如果我捕获实际在CURL请求中发送的标头信息,则输出如下:

    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

有人可以解释为什么CURL修改了Accept和Content-Type参数吗?

非常感谢任何帮助。

布赖恩

2 个答案:

答案 0 :(得分:1)

您想要的内容由CURLOPT_HTTPHEADER而不是CURLOPT_HEADER定义。

来自PHP Docs

  

CURLOPT_HEADER TRUE以在输出中包含标题。

     

CURLOPT_HTTPHEADER 要设置的HTTP标头字段数组,格式为数组('Content-type: text/plain', 'Content-length: 100')

有关使用它的信息,请参阅此user note

答案 1 :(得分:0)

您使用了设置内容类型的curl_setopt($ch, CURLOPT_POST, true);。而是使用curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");(请参阅http://php.net/manual/en/function.curl-setopt.php)。

这是@Alan Machado所说的。

相关问题