Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5

Author: Robin Nixon
This Month Stack Overflow 1


by anonymous   2018-07-19

With PHP you can get the job done by opening the url with a file_get_contents() and by using a preg_match() with regex.

Then, taking a random article page and browsing its html code, for example https://www.amazon.com/Learning-PHP-MySQL-JavaScript-jQuery/dp/1491978910/

By inspecting the code we can see the price is in a specific <span>:

<span class="a-size-medium a-color-price header-price"> 

Then we can do something like:

$link = 'https://www.amazon.com/Learning-PHP-MySQL-JavaScript-jQuery/dp/1491978910/';
$page_content = file_get_contents($link);

if(preg_match('/<span class=\"a-size-medium a-color-price header-price\">(.*?)<\/span>/i',
    $page_content, $matches)) {

    $price = trim($matches[1]);
} else {
    echo "Price not found.";
    $price = 0;

Then you have here enough basis to repeat the operation to get all the other informations you need to fetch.

Notice that it's not the best relevent way to do this as it assumes the HTML patterns are always the same from an article to another. The best relevant way is to use their API if possible.