Jump to content

file_get_contents extremely long load time?


smerny

Recommended Posts

I have this simple code just to test/learn getting data from other webpages:

<?php
$raw = file_get_contents("http://services.runescape.com/m=itemdb_rs/Cactus_spine/viewitem.ws?obj=6016");

$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");

$content = str_replace($newlines, "", html_entity_decode($raw));

$start = strpos($content,'<b>Market price:</b> ')+21;

$end = strpos($content,'</span><span><b>Maximum price:',$start);

$marketprice = substr($content,$start,$end-$start);

echo $marketprice;
?>

 

this code is taking over 30 seconds to load... is this normal for file_get_contents? the page that i am getting contents from will load at a decent rate when i go directly to that webpage.... so it's not that the page i am getting contents from is slow.

if you're loading external pages you probably want to use curl. i do anyways for anything external.

 

Also if you switch servers, file_get_contents may not support external urls. by default i'm pretty sure it doesn't. you need to enable the fopen wrappers.

looked at the manual and there was way too much info on curl that didn't really do a good job at explaining what to do... searched google for curl screen scraping and found an example that i modified to fit my need... this is what i'm using now, works much faster. thanks.

 

 

$ch = curl_init() or die(curl_error());
curl_setopt($ch, CURLOPT_URL,"http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj=2355");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$raw=curl_exec($ch) or die(curl_error());
curl_close($ch); 

$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");

$content = str_replace($newlines, "", html_entity_decode($raw));

$start = strpos($content,'<b>Market price:</b> ')+21;

$end = strpos($content,'</span><span><b>Maximum price:',$start);

$marketprice = substr($content,$start,$end-$start);

echo $marketprice;

If you're using PHP5 and have access to it, I'd recommend using HttpRequest instead of cURL.

http://usphp.com/manual/en/book.http.php

 

I primarily use:

http://usphp.com/manual/en/class.httprequest.php

 

It's really just a cURL wrapper, but I find it easy to use.  :)

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.