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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.