简体   繁体   English

CURL修改请求标头

[英]CURL modifying request headers

I'm hoping someone can help me. 我希望有人能帮助我。 I'm sending an API request, via CURL, using PHP. 我正在使用PHP通过CURL发送API请求。 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: 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);

However if I trap the header info actually sent in the CURL request the output is as follows: 但是,如果我捕获实际在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

Can anyone explain why CURL has modified the Accept and Content-Type parameters? 谁能解释CURL为什么修改了Accept和Content-Type参数?

Any help greatly appreciated. 任何帮助,不胜感激。

Brian 布赖恩

What you want is defined by CURLOPT_HTTPHEADER instead of CURLOPT_HEADER . 您想要的是由CURLOPT_HTTPHEADER而不是CURLOPT_HEADER定义的。

From PHP Docs : PHP Docs

CURLOPT_HEADER TRUE to include the header in the output. CURLOPT_HEADER TRUE,以在输出中包含标题。

CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the format array ('Content-type: text/plain', 'Content-length: 100') CURLOPT_HTTPHEADER以格式数组('Content-type: text/plain', 'Content-length: 100')设置的HTTP标头字段的数组

See this user note about using it. 请参阅此用户说明

You used curl_setopt($ch, CURLOPT_POST, true); 您使用了curl_setopt($ch, CURLOPT_POST, true); which sets the content-type. 设置内容类型。 Instead use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 而是使用curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); (see http://php.net/manual/en/function.curl-setopt.php ). (请参阅http://php.net/manual/zh/function.curl-setopt.php )。

This is in addition to what @Alan Machado said. 这是@Alan Machado所说的补充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM