Hi, I am quite new to php, I've just tried writing my first script. I want a php programmer to look at this and tell me I haven't done too badly for my first attempt. Although, I am assuming I have have probably took the long ways around doing probably what is quite a simple task to pro's, so if there are any better methods I'd love to hear where I went wrong! What it does is checks if a product on a website is in stock, when it becomes available to purchase the script triggers an sms to my phone using an sms api from textmagic.com. I set up a cron job to execute the file every couple of minutes, it scans the set webpage for a predefined phrase, then it triggers the api to text me a message when it changes stock status. I use a file called 1.sms to store either 1 or 0, this is to ensure it only texts me the first time the item appears in stock and not every time, then it would text me once again once it goes out of stock. I use this same code several times in the one script to watch different products on different manufacturers websites (some of the products I'm buying tend to sell out within less than an hour of being released, and with no set release date having this will give me the edge in getting there first)
So yea, tell me what you think... it has literally took me all night to write!
(i have added an ipad 2 that is out of stock on currys.co.uk as an example)
<?php
header('Content-Type: text/plain');
date_default_timezone_set('GMT');
$sms_user = 'username'; // TEXT MAGIC USERNAME
$sms_pass = 'password'; // TEXT MAGIC API PASSWORD
$sms_phone = '9991234567'; // PHONE NUMBER TO RECEIVE ALERTS
$sms_url = 'https://www.textmagic.com/app/api?username='.$sms_user.'&password='.$sms_pass.'&cmd=send&phone='.$sms_phone.'&unicode=0&text=';
// CHECK 1 . START //
$product_name = 'Apple iPad 2 16GB'; // PRODUCT NAME
$check_for = 'Out Of Stock'; // SEARCH PAGE FOR THIS UNIQUE PHRASE
$url_to_check = 'http://www.currys.co.uk/gbuk/computing/ipad-tablets-and-ereaders/apple-ipad/apple-ipad-2-16-gb-3g-wifi-white-09933627-pdt.html'; // URL TO SEARCH
$filename = '1.sms'; // UNIQUE FILE TO KEEP TRACK OF SMS UPDATES
$msg_true = $product_name.' is '.$check_for;
$msg_false = $product_name.' is NOT '.$check_for;
$stock_check = file_get_contents($url_to_check);
$stock_available = file_get_contents($filename);
if(stripos($stock_check, $check_for)) { echo $msg_true;
if($stock_available == 1) { file_put_contents($filename, '0');
$msg_true = str_replace(" ", "+", $msg_true);
$text_msg = file_get_contents($sms_url.$msg_true);
echo ' '.$text_msg; } else { echo '. (Last notified: '.date("F d Y H:i:s", filemtime($filename)).')'; } }
else { echo $msg_false;
if($stock_available == 0) { file_put_contents($filename, '1');
$msg_false = str_replace(" ", "+", $msg_false);
$text_msg = file_get_contents($sms_url.$msg_false);
echo ' '.$text_msg; } else { echo '. (Last notified: '.date("F d Y H:i:s", filemtime($filename)).')'; } }
// CHECK 1 . END //
?>