View Full Version : [SOLVED] retrieve xml file using file_get_contents($request); too slow with unicode

March 2nd, 2011, 06:38 PM
I am trying to retrieve an xml file from a webservice.
I cant use the original server since it gags on non latin characters, unicode?, that is what the LOC says.

The other server does work with non latin characters
BUT it is too slow with file_get_contents, takes a minute to get anything, so slow you cant even use it in the program.
YET, retrieves the xml fast in the browser window.

So, what can I do that will work normally fast like it should?

//original database server, file_get_contents is faster, about 3 to 10 seconds
//$request= 'http://z3950.loc.gov:7090/voyager?version=1.1&operation=searchRetrieve'.$query.$test2.'&startRecord='.$offset.'&maximumRecords=20';

//new one that can handle non latin characters
//file_get_contents is SLOW, so slow to be unusable, about a minute or more

$raw_xml = file_get_contents($request);
$filename = dirname(__FILE__)."/loc.xml";
$fp = fopen($filename, "w");
fwrite($fp, $raw_xml);
fclose ($fp);

March 2nd, 2011, 08:17 PM
got it working fast using cURL
to get cURL working in ubuntu, install it

sudo apt-get install php5-curl

restart apache
sudo /etc/init.d/apache2 restart

this works very fast! why does it work so good compared to file_get_contents ?

$url = curl_get_file_contents($request);

function curl_get_file_contents($URL)
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);

if ($contents) return $contents;
else return FALSE;

$filename = dirname(__FILE__)."/loc.xml";
//echo $raw_xml;
$fp = fopen($filename, "w");
fwrite($fp, $url);
fclose ($fp);