PHP: Check Website for Certain Text

For fun, I wanted to build a PHP script that checked a website to see if an item was in stock. I wanted the script to grab all the HTML on a web page and check to see if the words “Out of Stock” were anywhere on the page. Initially, I thought this would take a lot of work. But PHP makes it pretty easy with the use of the file_get_contents function. Here is an example of how I used it.

$itempage = file_get_contents('https://www.myonlinestore.com/item5345.html');

$exists = (strpos($itempage, 'Out of Stock') !== false);

if ($exists !== false) {
echo "Out of Stock";
}

else {
echo "In Stock";
}

The above code checks the web page at https://www.myonlinestore.com/item5345.html for the words “Out of Stock”. Note, that it is case sensitive. It will print on the page “Out of Stock” if the words “Out of Stock” are anywhere on the page. It the words are not found, it will print “In Stock” on the page.

Leave a comment

Your email address will not be published. Required fields are marked *