我可以使用 CURLOPT_HTTPHEADER 多次调用 curl_setopt 来设置多个标头吗? [英] Can I call curl_setopt with CURLOPT_HTTPHEADER multiple times to set multiple headers?

查看:36
本文介绍了我可以使用 CURLOPT_HTTPHEADER 多次调用 curl_setopt 来设置多个标头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 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:

  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆