I'm trying to grab a web page from a site using file_get_contents. When i pass a single url into file_get_contents(http://www.example.co.uk) it works fine. How ever i am using a while loop and passing in a url from a list in a text file. When i use this loop i get

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /drive2/fpgshttpd/definedwebdesign/get3.php on line 9

Warning: file_get_contents(http://www.google.com ): failed to open stream: No such file or directory in /drive2/fpgshttpd/definedwebdesign/get3.php on line 9

but the last url in the list will work and can be echo'd.

Please help its driving me mad i have tryed using fopen and fget instead of file_get_contents but i get the same error. The url has been passed to file_get_contents() correctly as it names the url in the error warning.

here's the code:

<?php
 $url='';
 $filename = "list.txt";
 $fp = fopen( $filename, "r" ) or die("Can't open File");
 while ( ! feof( $fp )  ) {
 $url = fgets($fp);
 $file = file_get_contents($url, "r");
 echo $file;
 }
?>

Thanks

Tim.

    It looks like you may need to use trim to clean the whitespace from arround the url...

    $fp = fopen(trim($filename), "r" ) or die("Can't open File");

      Nope, still gives me the same errors. Its strange because the last url in the list works.

      Tim.

        I don't know if this will solve the problem (I suspect not), but "file_get_contents()" doesn't take a read or write parameter. The second (optional) parameter is supposed to be a boolean indicating whether or not the include path should be used. In this case, the "r" is probably cast as true.

        What might solve your problem is using the "trim()" on the "fgets()" result:

        $url = trim(fgets($fp));

        (Notice the space in your error message after "google.com"--a line ending.)

          Thank you!!!
          That has fixed the problem!

          Tim.

            Write a Reply...