【问题标题】:how to parse a page that is going on 302 header?如何解析正在进行 302 标头的页面?
【发布时间】:2010-01-21 05:43:34
【问题描述】:

我必须在 php 中解析一个页面,该页面的 url 正在进行 302 临时移动标题并移动到一个未找到的页面。它的数据可以通过 mozilla 的 firebug add on 的控制台选项手动检索。但是如果我尝试使用 php 解析它,它会给我一个未找到的页面作为回报。我该如何解析该页面请建议??

编辑: 我正在做这样的事情来获取页面的内容

$file_results = @fopen("http://www.the url to be parses","rb");
    $parsed_results='';
    if($file_results)
    {
        while ($data3 = fread($file_results,"125000"))
        $parsed_results .= $data3;
    }

【问题讨论】:

    标签: php parsing http-status-code-302


    【解决方案1】:

    您可以在被重定向时使用get_headers() 查找所有标头。

    $url = 'http://google.com';
    $headers = get_headers($url, 1);
    
    print 'First step gave: ' . $headers[0] . '<br />';
    
    // uncomment below to see the different redirection URLs
    // print_r($headers['Location']);
    
    // $headers['Location'] will contain either the redirect URL, or an array
    // of redirection URLs
    $first_redirect_url = isset($headers['Location'][0]) ?
        $headers['Location'][0] : $headers['Location'];
    
    print "First redirection is to: {$first_redirect_url}<br />";
    
    // assuming you have fopen wrappers enabled...
    print file_get_contents($first_redirect_url);
    

    然后继续寻找直到你得到你想要的资源?

    【讨论】:

    • 是的,但这不是我想要的......看我想要第一页的内容而不是重定向页面的内容
    【解决方案2】:

    您需要阅读标头,查看它会将您重定向到哪里,然后发出另一个请求以获取实际资源。有点痛苦,但这就是协议的工作方式。大多数浏览器都透明地执行此操作。

    【讨论】:

    • 如果您能引用一些例子,我将不胜感激
    • 您最初使用什么方法提出请求?
    • 我不确定在 php 中执行此操作的最佳方法,但这是获取实际资源的一般思路。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多