【问题标题】:How to use PHP to get a webpage into a variable如何使用PHP将网页放入变量中
【发布时间】:2010-10-16 03:08:34
【问题描述】:

我想从网络上下载一个页面,当您使用像 Firefox 这样的简单浏览器时允许这样做,但是当我使用“file_get_contents”时,服务器拒绝并回复说它理解命令但不允许这样下载。

那该怎么办?我想我在一些脚本(在 Perl 上)中看到了一种通过创建用户代理和 cookie 使您的脚本像一个真正的浏览器的方法,这使得服务器认为您的脚本是一个真正的 Web 浏览器。

有没有人对此有想法,怎么做?

【问题讨论】:

    标签: php authentication curl file-get-contents


    【解决方案1】:

    使用 CURL。

    <?php
            // create curl resource
            $ch = curl_init();
    
            // set url
            curl_setopt($ch, CURLOPT_URL, "example.com");
    
            //return the transfer as a string
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    
            // set the UA
            curl_setopt($ch, CURLOPT_USERAGENT, 'My App (http://www.example.com/)');
    
            // Alternatively, lie, and pretend to be a browser
            // curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
    
            // $output contains the output string
            $output = curl_exec($ch);
    
            // close curl resource to free up system resources
            curl_close($ch);     
    ?>
    

    (来自http://uk.php.net/manual/en/curl.examples-basic.php

    【讨论】:

    • 好!虽然我需要脚本告诉服务器我正在使用浏览器,但仍然无法正常工作
    • 哦,抱歉 - 只需为 UA 添加 curl_setopt - 我已将其添加到我的答案中。
    【解决方案2】:

    是的,CUrl 在获取页面内容方面非常出色。我将它与 DOMDocumentDOMXPath 等类一起使用,以将内容研磨成可用的形式。

    function __construct($useragent,$url)
        {
            $this->useragent='Firefox (WindowsXP) - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.'.$useragent;
            $this->url=$url;
    
    
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_FAILONERROR, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_AUTOREFERER, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            $html= curl_exec($ch);
            $dom = new DOMDocument();
            @$dom->loadHTML($html);
            $this->xpath = new DOMXPath($dom);
        }
    ...
    public function displayResults($site)
    $data=$this->path[0]->length;
        for($i=0;$i<$data;$i++)
        {   
        $delData=$this->path[0]->item($i);
    
        //setting the href and title properties 
    $urlSite=$delData->getElementsByTagName('a')->item(0)->getAttribute('href'); 
                    $titleSite=$delData->getElementsByTagName('a')->item(0)->nodeValue;
    
        //setting the saves and additoinal
                      $saves=$delData->getElementsByTagName('span')->item(0)->nodeValue;
        if ($saves==NULL)
        {
            $saves=0;
        }
    
        //build the array
        $this->newSiteBookmark[$i]['source']='delicious.com';
        $this->newSiteBookmark[$i]['url']=$urlSite;
        $this->newSiteBookmark[$i]['title']=$titleSite;
        $this->newSiteBookmark[$i]['saves']=$saves;
    
    
                    }
    

    后者是从 delicious.com 抓取数据的类的一部分。虽然不是很合法。

    【讨论】:

    • 这是完全合法的,数据已经可用,只是一种低效的方式(HTML 不是最好的数据格式)。最近一直希望美味提供更多的 XML 数据(即搜索结果)。
    • 好吧,我希望delicious 为其API 提供一种方法,该方法实际上可以访问并非来自您自己的个人资料的书签,例如ma.gnolia.org“bookmark_find”方法。这样我的学士论文就可以节省一些不眠之夜了:=)
    【解决方案3】:

    此答案将您对 Rich 答案的评论牢记在心。

    该站点可能正在使用 HTTP 引用或用户代理字符串检查您是否是真实用户。尝试为您的 curl 设置这些:

     //pretend you came from their site already
    curl_setopt($ch, CURLOPT_REFERER, 'http://domainofthesite.com');
     //pretend you are firefox 3.06 running on windows Vista
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6');
    

    【讨论】:

      【解决方案4】:

      另一种方法(尽管其他人指出了更好的方法)是使用 PHP 的 fopen() 函数,如下所示:

      $handle = fopen("http://www.example.com/", "r");//open specified URL for reading
      

      如果 cURL 不可用,它特别有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多