0

我正在使用 simple_html_dom.php 从 url 获取所有图像(就像 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

4

1 回答 1

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);
于 2012-07-05T11:43:58.573 回答