문제

웹에서 페이지를 다운로드하고 싶습니다. Firefox와 같은 간단한 브라우저를 사용할 때 수행 할 수 있지만 "File_Get_Contents"를 사용할 때 서버는 명령을 이해하지만 그러한 다운로드를 허용하지 않는다고 대답합니다.

그래서 뭘 할건데? 일부 스크립트 (Perl)에서 사용자 에이전트와 쿠키를 만들어 스크립트를 실제 브라우저처럼 만들 수있는 방법을 보았습니다. 이는 서버가 스크립트가 실제 웹 브라우저라고 생각하게합니다.

누구든지 이것에 대한 아이디어, 어떻게 할 수 있습니까?

도움이 되었습니까?

해결책

컬을 사용하십시오.

<?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)

다른 팁

예, 컬은 페이지 콘텐츠를 얻는 데 꽤 좋습니다. 나는 그것을 같은 수업과 함께 사용합니다 domdocument 그리고 domxpath 컨텐츠를 사용 가능한 형태로 연마합니다.

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 그래도 합법적 인 것은 아닙니다.

이 답변은 귀하의 의견을 Rich의 답변을 염두에두고 있습니다.

이 사이트는 아마도 HTTP 참조 또는 사용자 에이전트 문자열을 사용하는 실제 사용자인지 여부를 확인하고있을 것입니다. 컬을 위해 이것들을 설정해보십시오.

 //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');

다른 방법으로 (다른 사람들이 더 나은 방법을 지적했지만) PHP의 fopen () 함수를 사용하는 것입니다.

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

컬을 사용할 수없는 경우 특히 유용합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top