0

I am trying to fetch prices from play and amazon for a personal project, but i have 2 problems. Firstly i have got play to work, but it fetches the wrong price, and secondly amazon doesnt fetch any results.

Here is the code i have been trying to get working.

$playdotcom = file_get_contents('http://www.play.com/Search.html?searchstring=".$getdata[getlist_item]."&searchsource=0&searchtype=r2alldvd');
$amazoncouk = file_get_contents('http://www.amazon.co.uk/gp/search?search-alias=dvd&keywords=".$getdata[getlist_item]."');

preg_match('#<span class="price">(.*)</span>#', $playdotcom, $pmatch);
$newpricep = $pmatch[1];
preg_match('#used</a> &nbsp;from&nbsp; <strong>(.*)</strong>#', $playdotcom, $pmatch);
$usedpricep = $pmatch[1];

preg_match('#<span class="bld lrg red"> (.*)</span>#', $amazoncouk, $amatch);
$newpricea = $amatch[1];
preg_match('#<span class="price bld">(.*)</span> used#', $amazoncouk, $amatch);
$usedpricea = $amatch[1];

then echo the results:

echo "Play :: New: $newpricep - Used: $usedpricep";
echo "Amazon :: New: $newpricea - Used: $usedpricea";

Just so you know whats going on

$getdata[getlist_item] = "American Pie 5: The Naked Mile";

which is working fine.

Any idea why these aren't working correctly?

EDIT: I have just realised that $getdata[getlist_item] in the file_get_contents is not using the variable, just printing the variable as is... why is it doing that???

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
FoxyFish
  • 874
  • 1
  • 12
  • 21
  • 2
    *Sidenote* [Dont use regex for HTML parsing](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags), use a proper parser like [DOMDocument](http://www.php.net/manual/en/class.domdocument.php) or a tool like [simplehtmldom](http://simplehtmldom.sourceforge.net/) – Lawrence Cherone May 01 '14 at 13:14
  • possible duplicate of [Interpolation (double quoted string) of Associative Arrays in PHP](http://stackoverflow.com/questions/4738850/interpolation-double-quoted-string-of-associative-arrays-in-php) – Tom Fenech May 01 '14 at 13:19

2 Answers2

1

The quotes you are using aren't consistent! Both your opening and closing quotes need to be the same.

Try this:

$playdotcom = file_get_contents("http://www.play.com/Search.html?searchstring=".$getdata['getlist_item']."&searchsource=0&searchtype=r2alldvd");
$amazoncouk = file_get_contents("http://www.amazon.co.uk/gp/search?search-alias=dvd&keywords=".$getdata['getlist_item']);

As it were ".$getdata[getlist_item]." was considered part of the string as you never closed the single quote string you initiated.

  • Glad I could be of some help :) – Cedric Le Varlet May 01 '14 at 13:38
  • @FoxyFish You should really be putting quotes around the array key, like `$getdata['getlist_item']`. Unquoted string literals are a relic from old PHP versions. Also, there's no need to use concatenation at all, you can do `"http://www.play.com/Search.html?searchstring={$getdata['getlist_item']}&searchsource=0&searchtype=r2alldvd"` – Tom Fenech May 01 '14 at 13:40
  • What FoxyFish is saying is very correct. I missed that but you really should add them! EDIT: I have updated the code accordingly – Cedric Le Varlet May 01 '14 at 13:42
  • It wasn't @FoxyFish who said that, I included the name so (s)he was notified. Good that you have edited your answer anyway. – Tom Fenech May 01 '14 at 13:44
  • Sorry I am still new on Stackoverflow and answering questions whilst working can create confusion :P But credit where credit is due... Thanks Tom Fenech ;) – Cedric Le Varlet May 01 '14 at 13:46
  • No problem. See the "help" link next to the comments which explains how to use "@" to notify a previous commenter. – Tom Fenech May 01 '14 at 13:49
1

Use curl function with correct headers. Below code will read the any web pages and then use a proper parser DOMDocument or simpleHTMLDomParser tool for read price from html content

$playdotcom = getPage("http://www.play.com/Search.html?searchstring=".$getdata['getlist_item']."&searchsource=0&searchtype=r2alldvd");
$amazoncouk = getPage("http://www.amazon.co.uk/gp/search?search-alias=dvd&keywords=".$getdata['getlist_item']);

function getPage($url){
$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$options = array(
    CURLOPT_CUSTOMREQUEST  =>"GET",
    CURLOPT_POST           =>false,
    CURLOPT_USERAGENT      => $user_agent,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER         => false,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_ENCODING       => 'gzip',
    CURLOPT_AUTOREFERER    => true,
    CURLOPT_CONNECTTIMEOUT => 30000,
    CURLOPT_TIMEOUT        => 30000,
    CURLOPT_MAXREDIRS      => 10,
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
curl_close( $ch );
return $content;
}
Manibharathi
  • 945
  • 6
  • 18