Jump to content

Grabbing data using cURL


-Karl-

Recommended Posts

So I have this code

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

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

} else {

$item = $_GET['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,'_obj_sprite.gif?id=')+19;
$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");
}

if (strlen($_POST['item']) <= 3) {
	die ("Search must be longer than 3 characters");
}

$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($rw));

$startmin = strpos($cont,'<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>Maxm price:</b> ')+21;
$endmax = strpos($content,'</span><br><br>',$start);
$maxprice = substr($content,$stmax,$endmax-$startmax);

$startchange = strpos($content2,'</td><td><span ss="')+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.';

}
?>

 

This works fine, it grabs the data and displays it. However, I was wondering if there was a better way to do it rather than using strpos and substr. Maybe Regex? Because at the moment it only selects the first item. Instead of all the records which come up after the search has been carried out.

 

Am I also correct in guessing I'll need to use foreach, to display the information for each item? Or would it be better to use a while?

 

Any help is appreciated. The code has been changed to make it unusable.

Link to comment
Share on other sites

I'd just like to add that it collects data from two different sources.

 

Firstly, it grabs the minimum, market and maximum price, along with the itemid, this itemid is then used to grab the change in price (which is located on a different page to the rest of the information).

Link to comment
Share on other sites

well.. from above this line

preg_match_all('#alt="([^"]+)"/>([0-9]+)#', $content, $out);

You could do a foreach, change the nessecary lines to match below..

 

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

curl_close($change);

foreach($raw as $raw1) {
    preg_match_all('#alt="([^"]+)"/>([0-9]+)#', $content, $out);

   ...
}

Might work. It seems as if the curl query, is getting hte first result as its not looped out. So this may workl.

Link to comment
Share on other sites

Umm,

 

Say you search for Pot.

 

These are the important lines:

 

// First Item
<td><img src="http://services.runescape.com/m=itemdb_rs/3016_obj_sprite.gif?id=5354" alt="Plant pot"></td> //Contains the item id and the name
<td><a href="http://services.runescape.com/m=itemdb_rs/Plant_pot/viewitem.ws?obj=5354"> Plant pot</a></td> //Contains the link to more specific information on the item
<td>199</td> //Contains the current price
<td><span class="stay">0</span></td> //contains the change in price

//Second Item
<td><img src="http://services.runescape.com/m=itemdb_rs/3016_obj_sprite.gif?id=4440" alt="Pot lid"></td> //Contains the item id and the name
<td><a href="http://services.runescape.com/m=itemdb_rs/Pot_lid/viewitem.ws?obj=4440"> Pot lid</a></td> //Contains the link to more specific information on the item
<td>9</td>  //Contains the current price
<td><span class="stay">0</span></td> //contains the change in price

 

So I need to make a regex for each of those lines, as they're constant throughout the web page. The parts that change are the item name, item id, current price, and the change in price. For the change in price the variable is the class name, which can be rise, fall, or stay, and the number following. But I'm rubbish with Regex :S

 

 

 

 

Link to comment
Share on other sites

Guest
This topic is now 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.