Php 修改请求头
我希望有人能帮助我。我正在使用PHP通过CURL发送一个API请求。第三方应用程序的标题需要如下所示:Php 修改请求头,php,curl,Php,Curl,我希望有人能帮助我。我正在使用PHP通过CURL发送一个API请求。第三方应用程序的标题需要如下所示: $headers = array( "content-type: application/json", "Accept: application/json" ); // 1. initialize $ch = curl_init(); // 2. set the options, including the url
$headers = array(
"content-type: application/json",
"Accept: application/json"
);
// 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);
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请求按如下方式初始化和发送:
$headers = array(
"content-type: application/json",
"Accept: application/json"
);
// 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);
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请求中实际发送的头信息,则输出如下:
$headers = array(
"content-type: application/json",
"Accept: application/json"
);
// 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);
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参数吗
非常感谢您的帮助
Brian您想要的是由
CURLOPT\u HTTPHEADER
定义的,而不是CURLOPT\u HEADER
发件人:
CURLOPT_HEADER为TRUE,将该HEADER包括在输出中
CURLOPT_HTTPHEADER要设置的HTTP头字段数组,格式为数组('Content-type:text/plain','Content-length:100')
关于使用它,请参见此。您使用了curl\u setopt($ch,CURLOPT\u POST,true)设置内容类型的代码>。使用curl_setopt($ch,CURLOPT_CUSTOMREQUEST,“POST”)代码>(请参阅)
这是对@Alan Machado所说内容的补充