FoxyFish
FoxyFish

Reputation: 892

file_get_contents not fetching the correct results

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

Upvotes: 0

Views: 193

Answers (2)

Manibharathi
Manibharathi

Reputation: 945

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;
}

Upvotes: 1

Cedric Le Varlet
Cedric Le Varlet

Reputation: 99

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.

Upvotes: 1

Related Questions