Powerlifter Powerlifter - 3 months ago 11
PHP Question

PHP/cURL post, just returns the page as if no post was made

My goal is to get the prices from this site: http://nl.myprotein.com/sports-nutrition/impact-whey-protein/10530943.html.

There is a price field on the site that is edited once a user selects a certain option

Option code:

<form action="10530943.html" method="post" class="field">
<fieldset>
<legend>Hoeveelheid</legend>
<label for="opts-7" class="">Hoeveelheid</label>
<select name="option" id="opts-7">
<option value="5859" selected>1 kg</option>
<option value="5913">2.5 kg</option>
<option value="5935">5 kg</option>
</select>
<input type="hidden" name="variation" value="7"/>
</fieldset>
</form>


This is the area where the price is updated once a selection box has been chosen:

<h2 class="price">
<span itemprop="price">&euro;15,99</span>
</h2>


Im using cURL to post the form to the aforementioned URL with this PHP code:

$URL = "http://nl.myprotein.com/sports-nutrition/impact-whey-protein/10530943.html";

$c = curl_init();
$agent = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';
curl_setopt($c, CURLOPT_USERAGENT, $agent);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
curl_setopt($c, CURLOPT_POST, true);

$data = array(
'option' => '5935',
'variation' => '7'
);

curl_setopt($c, CURLOPT_POSTFIELDS, $data);

$contents = curl_exec($c);
$info = curl_getinfo($c);
curl_close($c);


The website just responds as if no post was made. The first option is selected regardless of my input. How is this possible? Obviously something is wrong here. Is it because the way they handle the post-data am I missing something here?

Answer

This is a request which was sent by my browser to get information about price.

POST /variations.json?productId=10530943 HTTP/1.1
Host: nl.myprotein.com
Connection: keep-alive
Content-Length: 88
Origin: http://nl.myprotein.com
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
ADRUM: isAjax:true
Referer: http://nl.myprotein.com/sports-nutrition/impact-whey-protein/10530943.html

selected=3&variation1=5&option1=2408&variation2=6&option2=2407&variation3=7&option3=5913

I removed some browser-specific headers. If you use obtained information, you should be able to invoke POST request on your own. Form data are on the last line of the request.

JSON was returned by the server:

{"selected-product-id":10530986,"price":"&euro;35,99","rrp":35.99,"rrpDisplay":"&euro;35,99","rrpSaving":"&euro;0,00","rrpSavingPercent":null,"title":"Impact Whey Protein - Naturel - Zak - 2.5 kg","images":[{"index":0,"type":"thumbnail","name":"productimg/0/70/70/43/10530943-1395068394-710040.jpg"},{"index":0,"type":"product","name":"productimg/0/130/130/43/10530943-1395068394-710040.jpg"},{"index":0,"type":"large","name":"productimg/0/180/180/43/10530943-1395068394-710040.jpg"},{"index":0,"type":"extralarge","name":"productimg/0/600/600/43/10530943-1395068394-710040.jpg"},{"index":0,"type":"small","name":"productimg/0/50/50/43/10530943-1395068394-710040.jpg"},{"index":0,"type":"extrasmall","name":"productimg/0/20/20/43/10530943-1395068394-710040.jpg"},{"index":0,"type":"raw","name":"productimg/0/270/270/43/10530943-1395068394-710040.jpg"},{"index":0,"type":"largeproduct","name":"productimg/0/300/300/43/10530943-1395068394-710040.jpg"},{"index":0,"type":"zoom","name":"productimg/0/960/960/43/10530943-1395068394-710040.jpg"},{"index":0,"type":"smallthumb","name":"productimg/60/60/10530943-1454294063056312.jpg"},{"index":0,"type":"smallprod","name":"productimg/100/100/10530943-1454294063056312.jpg"},{"index":0,"type":"list","name":"productimg/200/200/10530943-1454294063056312.jpg"},{"index":0,"type":"quickview","name":"productimg/350/350/10530943-1454294063056312.jpg"},{"index":0,"type":"carousel","name":"productimg/480/480/10530943-1454294063056312.jpg"}],"bulk-buy":[],"variations":[{"id":5,"variation":"Flavour","options":[{"id":2408,"name":"Naturel","value":"Unflavoured"}]},{"id":6,"variation":"Package","options":[{"id":2407,"name":"Pouch","value":"Pouch"}]},{"id":7,"variation":"Amount","options":[{"id":5913,"name":"2.5 kg","value":"2.5"}]}]}

Information about price of selected product is in price or rrp variable (price is with currency mark).

Comments