Php 发送标题中的跟随位置

Php 发送标题中的跟随位置,php,header,httpresponse,Php,Header,Httpresponse,我正在尝试获取标题响应,其中包含需要授权的链接的以下代码。但是响应头被临时移动了302,我用CURLOPT_FOLLOWLOCATION尝试了curl,响应头是200 ok 那么我怎样才能使下面的代码像curl一样工作呢 function get_headers_x($url,$format=0, $user='', $pass='', $referer='') { if (!empty($user)) { $authentification = base64_encod

我正在尝试获取标题响应,其中包含需要授权的链接的以下代码。但是响应头被临时移动了
302
,我用
CURLOPT_FOLLOWLOCATION
尝试了curl,响应头是
200 ok

那么我怎样才能使下面的代码像curl一样工作呢

function get_headers_x($url,$format=0, $user='', $pass='', $referer='') {
    if (!empty($user)) {
        $authentification = base64_encode($user.':'.$pass);
        $authline = "Authorization: Basic $authentification\r\n";
    } else $authline = '';

    if (!empty($referer)) {
        $refererline = "Referer: $referer\r\n";
    } else $refererline = '';

    $url_info=parse_url($url);
    $port = isset($url_info['port']) ? $url_info['port'] : 80;
    $fp=fsockopen($url_info['host'], $port, $errno, $errstr, 30);
    if($fp) {
        $head = "GET ".@$url_info['path']."?".@$url_info['query']." HTTP/1.0\r\n";
        if (!empty($url_info['port'])) {
            $head .= "Host: ".@$url_info['host'].":".$url_info['port']."\r\n";
        } else {
            $head .= "Host: ".@$url_info['host']."\r\n";
        }
        $head .= "Connection: Close\r\n";
        $head .= "Accept: */*\r\n";
        $head .= "Location: 1 \r\n";
        $head .= $refererline;
        $head .= $authline;
        $head .= "\r\n";

        fputs($fp, $head);      
        while(!feof($fp) or ($eoheader==true)) {
            if($header=fgets($fp, 1024)) {
                if ($header == "\r\n") {
                    $eoheader = true;
                    break;
                } else {
                    $header = strtolower(trim($header));
                }

                if($format == 1) {
                    $ex = explode(':',$header);
                    $key = array_shift($ex);
                    if($key == $header) {
                        $headers[] = $header;
                    } else {
                        $headers[$key]=substr($header,strlen($key)+2);
                    }
                    unset($key);
                } else {
                    $headers[] = $header;
                }
            }
        }
        return $headers;

    } else {
        return false;
    }
}

您必须解析
$headers
,如果出现
位置:
,则必须发出新请求