I copy the php script in the web. I found that it has the following error massage “Warning: file(http://www.hko.gov.hk/textonly/forecast/chinesewx.htm) [function.file]: failed to open stream: HTTP request failed! in /var/www/html/weather1.php on line 18†when I use in my web server. However, it works if I change the code from “$weatherURL=' http://www.hko.gov.hk/textonly/forecast/chinesewx.htm';†to “$weatherURL=' text.html';†(text.html stored the text in the http://www.hko.gov.hk/textonly/forecast/chinesewx.htm). Can anyone help me. <?php $weatherURL=' http://www.hko.gov.hk/textonly/forecast/chinesewx.htm'; $file=file($weatherURL); $temperature = $file[15]; //line 15 $temperature = ereg_replace("氣 溫 : ","","$temperature"); $temperature = ereg_replace(" 度","","$temperature"); $temperature = ereg_replace("\n","","$temperature"); echo $temperature; ?>
The problem is that the file() function requires a path within the server. All you need to do is replace $file=file($weatherURL); with $file=file_get_contents($weatherURL);
The error is still happened. The error massage is "Warning: file_get_contents(http://www.hko.gov.hk/textonly/forecast/chinesewx.htm) [function.file-get-contents]: failed to open stream: HTTP request failed! in /var/www/html/weather1.php on line 18"
I use allways the php webclient class Snoopy to grap websites. But use the Snoopy class from wordpress, that's better maintenced than the original Snoopy class...
I think I see the problem, you need to take out that extra space character at the beginning of the URL. Like so: $weatherURL = '"http://www.hko.gov.hk/textonly/forecast/chinesewx.htm";
I check that the outgoing connections is enabled and I use the code like "$weatherURL = '"http://www.hko.gov.hk/textonly/forecast/chinesewx.htm";". The problem is still happened.
If this happens to me I just use curl. $url="weathersite" $ch = curl_init(); $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; ". "Windows NT 5.0)"; $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); curl_setopt( $ch, CURLOPT_HTTPGET, 1 ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 ); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_REFERER, $ref ); curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); $html = curl_exec($ch); curl_close($ch); PHP: Then parse $html