0

page: http://www.nastygal.com/accessories/minnie-bow-clutch

code: $html = file_get_contents('http://www.nastygal.com/accessories/minnie-bow-clutch');

The $html always contains the USD price of the product even when I change the currency on the upper right of the page. How do I capture the html that has the CAD price when I change the currency of the page to CAD?

Commonboy
  • 41
  • 2
  • 6

2 Answers2

2

It seems as though the country and currency selection are stored in cookies.

enter image description here

I'm assuming you're going to have to pass those values along with your file_get_contents() call. See: PHP - Send cookie with file_get_contents


EDIT #1

To follow up on my comment, I just tested this:

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: CURRENCYPREFERENCE=cad\r\n" 
  )
);

$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.nastygal.com/accessories/minnie-bow-clutch', false, $context);
print_r($file);

And was able to get this:

enter image description here


EDIT #2:

In response to your second comment. Those were important details. What does your bookmarklet do with the scraped contents? Are you saving a copy of the bookmarked product page on your own website? Regardless, you're going to have to modify your bookmarklet to check the user's cookies before submitting the request to run file_get_contents().

I was able to access my cookies from nastygal.com using the following simple bookmarklet example. Note: nastygal.com uses jQuery and the jQuery UI cookie plugin. If you're looking for a more generic solution, you should not rely on these scripts being there:

javascript:(function(){ console.log($.cookie('CURRENCYPREFERENCE')); }());

Output in the JS console:

cad
Community
  • 1
  • 1
labue
  • 2,605
  • 3
  • 26
  • 35
  • thanks for the pointer, but how do I go about getting this cookie value so I can pass it into php? I've tried retrieving it using js document.cookie but couldn't find it. – Commonboy Jan 25 '13 at 23:16
  • @AsdfasfdFasdfsdf You don't have to 'get' the cookie value. Using the example I linked or the PHP page DavidHamp linked, you can pass an array of key=value pairs to mimic the cookies normally set by nastygal.com. – labue Jan 25 '13 at 23:53
  • Sry, just to clarify what I'm trying to do, users will be using my bookmarklet from their browser to extract the price, like how kaboodle does it. Example use case: 1. User goes to the product page (price now shows USD) 2. They change the currency to AUD (price now changed to AUD) 3. They click on my bookmarklet from their browser 4. The bookmarklet intelligently scraps the AUD price off. I'm still stuck on determining WHICH currency they selected so I can pass the right cookie value with the HTTP request to get the HTTP response containing the chosen currency price. Hope that makes sense! – Commonboy Jan 26 '13 at 04:20
  • Hi mofolo, I'm still having no luck getting the cookie value using $.cookie('CURRENCYPREFERENCE'); it returns null. I then check the list of cookies in $.cookie() and they are the same set in document.cookie, what am I missing? – Commonboy Jan 27 '13 at 22:56
2

It looks like currency preferences are being saved in a cookie named: CURRENCYPREFERENCE

Since it's not your browser making the connection to retrieve that view, you're likely not sending any cookie data along with your request.

I believe example #4 here will get you what you need: http://php.net/manual/en/function.file-get-contents.php

David Hamp
  • 151
  • 3