Jump to content

How to speed this script up?


-Karl-

Recommended Posts

<?php
function utime (){
$time = explode( " ", microtime());
$usec = (double)$time[0];
$sec = (double)$time[1];

return $sec + $usec;
}

echo '<style type="text/css">* { font-family:Arial, Helvetica, sans-serif; }</style>'; 

$start1 = utime(); 

if(!isset($_POST['submit'])) {

echo '<form action="item.php" method="post">
	<input type="text" name="item" />
	<input type="submit" name="submit" value="Lookup" />
	</form>';

} else {

$item = $_POST['item'];

$change = curl_init() or die(curl_error());

curl_setopt($change, CURLOPT_URL,'http://services.runescape.com/m=itemdb_rs/results.ws?query=' . $item);
curl_setopt($change, CURLOPT_RETURNTRANSFER, 1);

$raw1 = curl_exec($change) or die(curl_error());

curl_close($change); 

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

$startitemid = strpos($content2,'<td><img src="http://services.runescape.com/m=itemdb_rs/2982_obj_sprite.gif?id=')+79;
$enditemid = strpos($content2,'" alt="',$startitemid);
$itemid = substr($content2,$startitemid,$enditemid-$startitemid);

$startitem = strpos($content2,'<td><a href="http://services.runescape.com/m=itemdb_rs/')+55;
$enditem = strpos($content2,'/viewitem.ws?obj=',$startitem);
$item = substr($content2,$startitem,$enditem-$startitem);

$startcheckitem = strpos($content2,'Your search for ')+30;
$endcheckitem = strpos($content2,'. You should check the spelling',$startcheckitem);
$checkitem = substr($content2,$startcheckitem,$endcheckitem-$startcheckitem);

if (strstr($checkitem,'did not')) {
	die ("No results");
}

$ch = curl_init() or die(curl_error());

curl_setopt($ch, CURLOPT_URL,'http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj=' . $itemid);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$raw = curl_exec($ch) or die(curl_error());

curl_close($ch); 

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

$startmin = strpos($content,'<b>Minimum price:</b> ')+21;
$endmin = strpos($content,'</span><span class="spaced_span">',$start);
$minprice = substr($content,$startmin,$endmin-$startmin);

$startmarket = strpos($content,'<b>Market price:</b> ')+21;
$endmarket = strpos($content,'</span><span><b>Maximum price:',$start);
$marketprice = substr($content,$startmarket,$endmarket-$startmarket);

$startmax = strpos($content,'<b>Maximum price:</b> ')+21;
$endmax = strpos($content,'</span><br><br>',$start);
$maxprice = substr($content,$startmax,$endmax-$startmax);

$startchange = strpos($content2,'</td><td><span class="')+28;
$endchange = strpos($content2,'</span></td><td>',$startchange);
$changeprice = substr($content2,$startchange,$endchange-$startchange);


$theitem = str_replace("_", " ", $item);

if (strstr($changeprice,'+')) {
	$fontcolor = "green";
}
if (strstr($changeprice,'-')) {
	$fontcolor = "red";
}
if (!strstr($changeprice,'-') && !strstr($changeprice,'+')) {
	$fontcolor = "blue";
}

echo '<table>
	<tr>
		<td>Item:</td>
		<td>' . $theitem . '</td>
	</tr>

	<tr>
		<td>Minimum Price:</td>
		<td>' . $minprice . '</td>
	</tr>

	<tr>
		<td>Market Price:</td>
		<td>' . $marketprice . '</td>
	</tr>

	<tr>
		<td>Maximum Price:</td>
		<td>' . $maxprice . '</td>
	</tr>

	<tr>
		<td>Change:</td>
		<td><font color="' . $fontcolor . '">' . $changeprice . '</font></td>
	</tr>
</table>

<br /><br />';

$end = utime();
$run = $end - $start1;

echo 'Page loaded in ' . substr($run, 0, 5) . ' seconds.';

}
?>

 

Pretty much taking 2-2.5 seconds to load on average, any ideas?

Link to comment
https://forums.phpfreaks.com/topic/200692-how-to-speed-this-script-up/
Share on other sites

Let's say you get a request to fetch details about item called 'A powdered wig'. Your script can fetch this information from runescape's site, display it to user, but also store it in your database. Now, next time your script gets a request about 'A powdered wig' it can check if it has this information in database, and how old it is. If it is say, less than 6 hours old (don't know how often values on runescape's site change), use the information from database, otherwise fetch infromation from runescape (and update your database)

Karl my man, this is how you could do what mchl said.

 

You could do a query at the bottom.. under the echos, or just above them.. to insert the specific data you need into the database with a date inserted timestamp.

 

Somewhere along the line.. you turn the textbox value, into an item name.

When this happens, you'd do a select query based on that item name.

If rows are found, check if the timestamp isn't 6 or more hours behind current time, display info from the database, else follow through with grabbing it from the remote server.

 

This may not be the best method, it probably can be improved.. although that'd pretty much how you'd do it.

Karl my man, this is how you could do what mchl said.

 

You could do a query at the bottom.. under the echos, or just above them.. to insert the specific data you need into the database with a date inserted timestamp.

 

Somewhere along the line.. you turn the textbox value, into an item name.

When this happens, you'd do a select query based on that item name.

If rows are found, check if the timestamp isn't 6 or more hours behind current time, display info from the database, else follow through with grabbing it from the remote server.

 

This may not be the best method, it probably can be improved.. although that'd pretty much how you'd do it.

 

Epic idea, I'll get to work on it soon :)

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.