tags:

views:

107

answers:

1

Hello, 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!

+1  A: 

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);
quantumSoup
I thought this was the problem, but why can it echo the $url variable right before the $blogpost variable assignment in the code above? I added an "echo $url;" just to test that, and the variable is there every time. It's like the $url value is lost when passed to simple_html_dom.php
RafaelM
Thanks Aircule, your post gave me an idea. I tried the explode with a "|" (pipe character) instead of a \n (newline) and it worked. Maybe a Windows newline character issue?
RafaelM
@RafaelM You may simply not notice the empty strings, as well, they're empty strings. Try wrapping the output of the echo with some `<pre>...</pre>` tags and check the source. Or, better yet, use `var_dump($url)` It'll give you more info about the variable, like it's type, the length of strings, etc.
George Marian
@RafaelM It *appeared* to work every time because when you echo an empty variable you don't see it ;) The problem is that it was generating more entries than there actually were, not that some entries disappeared.
quantumSoup
@RafaelM As @George suggested, here are two very useful functions: [print_r](http://php.net/manual/en/function.print-r.php) and [var_dump](http://php.net/manual/en/function.var-dump.php). Make sure to use `<pre>` tags.
quantumSoup