Jump to content

SMS product in stock alert script


antminer

Recommended Posts

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 //


 ?>
 
Edited by antminer
Link to comment
Share on other sites

Ok... I spoke far too soon..

 

It seems this code is working as expected when I visit it through a web browser, but when I set up a cron job i think the file_put_contents($filename, '0'); won't change from the 1 to 0.. Which means when the product arrives in stock the email is constantly being sent every few minutes until out of stock again.

 

Does anyone know what the problem could be?  would it possibly be something to do with the "header('Content-Type: text/plain');" at the start not displaying because its a cron job and maybe doesn't display headers or something???

 

Please advise, i'm lost at this stage.

Link to comment
Share on other sites

I am nearly positive the problem is with the header('Content-Type: text/plain');

 

I used this because of the $stock_check = file_get_contents($url_to_check) downloads the html from the webpage, and the stripos($stock_check, $check_for) wouldn't work unless it was text/plain

 

so i guess my question is, how can i get $stock_check = file_get_contents($url_to_check) to output as plain text rather than html without using that header command?

Link to comment
Share on other sites

I have solved the problem above by removing the header and converting the variables seperately... but this didn't fix the problem when running in cron job. So i'm back to the same problem in post 2.

 

Again i'm thinking the problem is with file_put_contents not writing data to the 1.sms file when in a cron.  Has anyone got any ideas why this wouldnt be writing. I have already checked file/folder permissions and all seem ok.

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.