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

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

问题描述

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

  $ 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);


方法在中的这个回答Php - 调试卷曲 )回答了问题:不,不能使用 CURLOPT_HTTPHEADER 调用 curl_setopt 。第二个调用将覆盖第一个调用的头。



相反,函数需要调用一次所有头文件:

  $ headers = array(
'Content-type:application / xml',
'授权:gfhjui',
);
curl_setopt($ curlHandle,CURLOPT_HTTPHEADER,$ headers);相关(但不同的)问题是:



  • 如何通过curl调用使用HTTP请求发送标头?(在命令行上卷曲)

  • 如何获取先前使用curl_setopt()设置的选项?(curl PHP扩展)


  • 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);
    

    解决方案

    Following what curl does internally for the request (via the method outlined in this answer to "Php - Debugging Curl") answers the question: No, it is not possible to use the curl_setopt call with CURLOPT_HTTPHEADER. The second call will overwrite the headers 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:

    这篇关于我可以用CURLOPT_HTTPHEADER多次调用curl_setopt来设置多个头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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