Problem with SimpleHTMLDom
I've run in to a problem with a PHP script using SimpleHTMLDOM to pull a list of URLs of a page.
If I specify the URL I want to read the links off, the script gives me no problems:
开发者_运维技巧 $url='http://www.example.com';
$blogpost = file_get_html($url);
foreach ($blogpost->find('a[href*=example1]') as $example1link) {
$example1link = $example1link->href;
echo $example1link;
}
All this does is pull from www.example.com all the links to www.example1.com and echo it back to me.
But when I try to feed the script a text file with URLs:
$urlarray = split("\n", file_get_contents('urls.txt'));
foreach ($urlarray as $url) {
$blogpost = file_get_html($url);
foreach ($blogpost->find('a[href*=example1]') as $example1link) {
$example1link = $example1link->href;
echo $example1link;
}
}
It gives me the following error:
Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty
in simple_html_dom.php on line 39
For those that don't have simple_html_dom.php this is the function the error refers to:
function file_get_html() {
$dom = new simple_html_dom;
$args = func_get_args();
$dom->load(call_user_func_array('file_get_contents', $args), true);
return $dom;
}
I can even echo $url right before I assign the value to $blogpost. The problem seems to be in passing the $url variable to file_get_html(). But only when I use a txt file with target links to scrape.
I'm very new to PHP (and programming in general) and I've searched around almost all day and cannot find what I'm doing wrong.
Any help is appreciated.
Thanks!
Well, it means just what it says: you are trying to pass and empty string to the file_get_contents function, which is probably being called by file_get_html. This is likely because when you use split() (which is, by the way, deprecated - use explode instead), you will generate an array that has empty strings in some entries.
You can simply swallow the error using error suppression (ie: $blogpost = @file_get_html(...)
) or make sure you don't pass empty string to your method, ie:
if (!empty($url))
$blogpost = file_get_html($url);
精彩评论