我可以使用 CURLOPT_HTTPHEADER 多次调用 curl_setopt 来设置多个标头吗? [英] Can I call curl_setopt with CURLOPT_HTTPHEADER multiple times to set multiple headers?
问题描述
我可以使用 CURLOPT_HTTPHEADER
多次调用 curl_setopt
来设置多个标头吗?
Can I call curl_setopt
with CURLOPT_HTTPHEADER
multiple times to set multiple headers?
$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);
推荐答案
遵循 curl 在内部为请求所做的事情(通过 这个对Php - Debugging Curl"的回答)回答了这个问题:否.
Following what curl does internally for the request (via the method outlined in this answer to "Php - Debugging Curl") answers the question: No.
不,不可能将 curl_setopt
调用与 CURLOPT_HTTPHEADER
一起使用多次,每次都传递一个标头,以便设置多个标头.
No, it is not possible to use the curl_setopt
call with CURLOPT_HTTPHEADER
more than once, passing it a single header each time, in order to set multiple headers.
第二次调用将覆盖前一次调用的标头(例如第一次调用的标头).
A second call will overwrite the headers of a previous call (e.g. of the first call).
相反,该函数需要使用所有标头调用一次:
Instead the function needs to be called once with all headers:
$headers = array(
'Content-type: application/xml',
'Authorization: gfhjui',
);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);
相关(但不同)的问题是:
Related (but different) questions are:
- 如何发送通过 curl 调用使用 HTTP 请求的标头?(命令行上的 curl)
- 如何获取之前使用 curl_setopt() 设置的选项?(curl PHP 扩展)
- How to send a header using a HTTP request through a curl call? (curl on the commandline)
- How to get an option previously set with curl_setopt()? (curl PHP extension)
这篇关于我可以使用 CURLOPT_HTTPHEADER 多次调用 curl_setopt 来设置多个标头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!