Jump to content

file_get_contents() works sometimes


whitepaint

Recommended Posts

Greetings,

 

I am using file_get_contents() to obtain data for an indexing project I am doing.  The data I am using is from Wikipedia.  With some URLs, there is no issue, with others, the function returns nothing.  The first example below works; The second example does not.

 

http://en.wikipedia.org/wiki/USS_Enterprise_%28CVN-65%29

 

http://en.wikipedia.org/wiki/SMS_Gro%C3%9Fer_Kurf%C3%BCrst_%281913%29

 

When I call the function, I print the data immediately.  Any ideas out there with what is going on?  Any help is appreciated.

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/114143-file_get_contents-works-sometimes/
Share on other sites

I'm not sure why, but using file_get_contents on the second url failed in my tests. Using cURL worked:

<?php
$urls = array('http://en.wikipedia.org/wiki/USS_Enterprise_%28CVN-65%29', 'http://en.wikipedia.org/wiki/SMS_Gro%C3%9Fer_Kurf%C3%BCrst_%281913%29');
foreach ($urls as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);      
curl_close($ch);
echo $output;
}?>

 

Ken

Thanks fot the help, I am using your code now.

I'm not sure why, but using file_get_contents on the second url failed in my tests. Using cURL worked:

<?php
$urls = array('http://en.wikipedia.org/wiki/USS_Enterprise_%28CVN-65%29', 'http://en.wikipedia.org/wiki/SMS_Gro%C3%9Fer_Kurf%C3%BCrst_%281913%29');
foreach ($urls as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);      
curl_close($ch);
echo $output;
}?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.