duanhanglekr37902 2014-09-25 07: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 07: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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部