Simple_html_dom:无法从大型网站接收图片?

时间:2012-07-05 11:26:49

标签: php simple-html-dom

我正在使用simple_html_dom.php从网址获取所有图片(如pinterest一样)

if($_POST['submit']) {

$url = $_POST['form_url'];
$html = file_get_html($url);
$count = 0;
$goodfiles = array();

    if($html && is_object($html) && isset($html->nodes)){
        foreach($html->find('img') as $img){
            $count++;
        }
    }else{
        echo "failed";
    }

echo $count;

}

}

对于很多网站,我收到了网站上有多少图片的数量。但是例如对于网站pinterest.com我收到以下错误:

 Warning: file_get_contents(http://www.pinterest.com) [function.file-get-contents]: failed to open stream: Connection timed out in /home/vyrxkian/domains/bblablabla/include/simple_html_dom.php on line 70
 failed 0

当我进一步指定错误时,我得到了这个:

Warning: file_get_contents(http://www.pinterest.com) [function.file-get-contents]: failed to open stream: Connection timed out in /home/vyrxkian/domains/bblablabla/include/simple_html_dom.php on line 70
 Fatal error: Call to a member function find() on a non-object in /home/vyrxkian/domains/bblablabla.php on line 30

如何保护此错误并阅读例如pinterest.com

1 个答案:

答案 0 :(得分:0)

您可以使用CURL库:

$url =  $_POST['form_url'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 7); 
$resultHtml = curl_exec($ch); // run the whole process
curl_close($ch);

$html = new simple_html_dom();
$html->load($resultHtml);