php - CURL修改请求头

标签 php curl

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

    $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 请求中实际发送的 header 信息,则输出如下:

    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参数?

非常感谢任何帮助。

布莱恩

最佳答案

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

来自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')

查看此user note关于使用它。

关于php - CURL修改请求头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32657069/

相关文章:

php - 如何在 PHP 中从 .pfx 证书获取 key.pem 和 crt.pem

php - Swiftmailer:按用户 ID 进行 dbReplacements?

php - 选择出现在 MySQL 中 2 个表上的数据

javascript - 如何编写一个 Node.js Express 服务器来接收 curl POST

bash - 百分号 % 在 crontab 中不起作用

linux - 如何在 Linux 中使用 curl 或任何 shell 脚本或任何命令行实用程序将整个目录上传到 artifactory?

php - 在哪里存储子域 Assets ?

php - 启用 APC 后页面超时

php - 用不同的值更新一个数据库表列

php - 通过 cURL POST 导致 PHP 中的空数组