Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract data from website via PHP

I am trying to create a simple alert app for some friends.

Basically i want to be able to extract data "price" and "stock availability" from a webpage like the folowing two:

  • http://www.sparkfun.com/commerce/product_info.php?products_id=5
  • http://www.sparkfun.com/commerce/product_info.php?products_id=9279

I have made the alert via e-mail and sms part but now i want to be able to get the quantity and price out of the webpages (those 2 or any other ones) so that i can compare the price and quantity available and alert us to make an order if a product is between some thresholds.

I have tried some regex (found on some tutorials, but i an way too n00b for this) but haven't managed to get this working, any good tips or examples?

like image 709
Mike Avatar asked Jan 07 '10 11:01

Mike


2 Answers

$content = file_get_contents('http://www.sparkfun.com/commerce/product_info.php?products_id=9279');

preg_match('#<tr><th>(.*)</th> <td><b>price</b></td></tr>#', $content, $match);
$price = $match[1];

preg_match('#<input type="hidden" name="quantity_on_hand" value="(.*?)">#', $content, $match);
$in_stock = $match[1];

echo "Price: $price - Availability: $in_stock\n";
like image 95
Matteo Riva Avatar answered Oct 19 '22 17:10

Matteo Riva


It's called screen scraping, in case you need to google for it.

I would suggest that you use a dom parser and xpath expressions instead. Feed the HTML through HtmlTidy first, to ensure that it's valid markup.

For example:

$html = file_get_contents("http://www.example.com");
$html = tidy_repair_string($html);
$doc = new DomDocument();
$doc->loadHtml($html);
$xpath = new DomXPath($doc);
// Now query the document:
foreach ($xpath->query('//table[@class="pricing"]/th') as $node) {
  echo $node, "\n";
}
like image 41
troelskn Avatar answered Oct 19 '22 18:10

troelskn