我可以多次使用CURLOPT_HTTPHEADER调用curl_setopt来设置多个标头吗?

hak*_*kre 66 php curl

我可以打电话curl_setoptCURLOPT_HTTPHEADER多次设置多个标头?

$url = 'http://www.example.com/';

$curlHandle = curl_init($url);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array('Authorization: gfhjui'));

$execResult = curl_exec($curlHandle);
Run Code Online (Sandbox Code Playgroud)

hak*_*kre 100

遵循curl内部的请求(通过本答案中概述的方法"Php - Debugging Curl")回答问题:不,不可能使用curl_setopt调用CURLOPT_HTTPHEADER.第二个调用将覆盖第一个调用的标头.

相反,需要使用所有标头调用该函数一次:

$headers = array(
    'Content-type: application/xml',
    'Authorization: gfhjui',
);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);
Run Code Online (Sandbox Code Playgroud)

相关(但不同)的问题是:


小智 10

其他类型的格式:

$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-length: 0';

curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);
Run Code Online (Sandbox Code Playgroud)