duanhanglekr37902 2014-09-25 15:24
浏览 27
已采纳

CURLOPT_HEADER返回401

I need to get the location header. From what I've read, this should be as simple as

curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, false);

If I don't include those two options, the curl request works fine, but I'm not able to get the location header.

If I do include those two options, then I get a 401 error.

The Location that is being returned should be a URL that does require an additional login. What am I doing wrong?

If it makes a difference, I'm doing a PUT.

Update: Turns out I was looking too much at the trees and not enough at the forest. When generated the information needed for a response to this question I realized the issue was actually with the call prior to this call.

Before the PUT, I need to get a session token. Since I wasn't parsing out the headers when getting the session token, I was getting a blank session token which was resulting in the 401 for the PUT.

  • 写回答

1条回答 默认 最新

  • dongxi5494 2014-09-25 15:54
    关注

    HTTP response headers are included to the result of curl_exec when you set CURLOPT_HEADER option. So if you want to get response headers you have to extract it from the result using substr() and curl_getinfo(CURLINFO_HEADER_SIZE).

    Here is a sample:

    $c = curl_init();
    // setting options including CURLOPT_HEADER
    // ...
    $result = curl_exec($c);
    $info = curl_getinfo($c);
    curl_close($c);
    // extracting headers from result:
    $n = $info['header_size'];
    $headers = rtrim(substr($result, 0, $n));
    $content = substr($result, $n);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题