【问题标题】:tracking page headers and redirects with php-libcurl使用 php-libcurl 跟踪页面标题和重定向
【发布时间】:2011-08-29 12:13:56
【问题描述】:

我正在编写一个脚本来跟踪标头,尤其是 URL 的重定向和 cookie。 很多时候,当我打开一个 url 时,它会重定向到另一个 url,或者有时会重定向到多个 url,并且还会存储一些 cookie。但是当我用 url 运行脚本时

http://en.wikipedia.org/

我的脚本没有保存 cookie,它只显示了一个重定向并且没有存储任何 cookie。但是当我在 Firefox 中浏览 url 时,它保存了 cookie,当我用Live HTTP Headers 检查它时,它显示了多个获取请求。 Live HTTP Headers 还显示存在 Set-Cookie 标头。

<?php

$url="http://en.wikipedia.org/";
$userAgent="Mozilla/5.0 (Windows NT 5.1; rv:2.0)Gecko/20100101 Firefox/4.0";
$accept="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$encoding="gzip, deflate";
$header['lang']="en-us,en;q=0.5";
$header['charset']="ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header['conn']="keep-alive";
$header['keep-alive']=115;
$i=1;
$flag=1;        //0 if there is no redirect i.e. no location header to follow. used here to to control the while loop below

while($flag!=0) {
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_USERAGENT,$userAgent);
    curl_setopt($ch,CURLOPT_ENCODING,$encoding);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,0);
    curl_setopt($ch,CURLOPT_HEADER,1);
    curl_setopt($ch,CURLOPT_NOBODY,1);
    curl_setopt($ch,CURLOPT_AUTOREFERER,true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookie.txt");
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $pageHeader[$i]=curl_exec($ch);
    curl_close($ch);
    $flag=preg_match('/Location: (.*)\s/',$pageHeader[$i],$location[$i]);
    if($flag==1) {      //if there is a location header    
        if(preg_match('@^(http://|www.)@',$location[$i][1],$tempurl)==1) {      //if it is an absolute url
            $url=$location[$i][1];
        } else {
            if(preg_match('@^/(.*)@',$location[$i][1],$tempurl)==1) {   //if the url corresponds to url relative to server's root
                preg_match('@^((http://)|(www.))[^/]+@',$url,$domain);
                $url=$domain.$tempurl[0];
            } else {        //if the url is relative to current directory
                $url=preg_replace('@(/[^/]+)$@',"/".$location[$i][1],$url);
            }
        }
        $location[$i]=$url;
        preg_match('/Set-Cookie: (.*)\s/',$pageHeader[$i],$cookie[$i]);
        $i++;
    }

    foreach($location as $l)
        $loc=$loc.$l."\n";

    $header=implode("\n\n\n",$pageHeader);
    file_put_contents(dirname(__FILE__) . "/location.txt",$loc);
    file_put_contents(dirname(__FILE__) . "/header.txt",$header);
?>

这里创建了文件location.txt 和header.txt,但未创建cookie.txt。 如果我将 url 更改为 google.com,那么它会在 location.txt 文件中显示重定向到 google.co.in 并在 cookie.txt 文件中保存一个 cookie。但是当我在Firefox 中打开google.com 时,它会保存三个cookie。有什么问题? 我认为页面上有一些 javascript 正在设置 cookie,因此 curl 无法获取。 也欢迎对上述代码的改进提出任何建议

【问题讨论】:

    标签: php cookies redirect curl header


    【解决方案1】:

    您的位置:以下代码已完全损坏,因为您应该已经看到大多数 HTTP 重定向是相对的,因此您不能只将该字符串用作后续请求中的 URL。

    【讨论】:

    • Daniel Stenberg:我已经修复了相对 url 的代码,我认为 cookie 没有设置,因为 cookie 是由 html 页面中的 javascript 设置的。我会在考试后的几天内解决这个问题。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2019-07-22
    • 2015-02-11
    • 2020-11-17
    • 2011-05-26
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多