我希望有人能帮助我。我正在使用 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/