PHP / cURL帖子,只是返回页面,好像没有发布帖子

时间:2015-08-05 09:36:38

标签: php curl

我的目标是从此网站获取价格:http://nl.myprotein.com/sports-nutrition/impact-whey-protein/10530943.html

网站上有一个价格字段,一旦用户选择某个选项

,就会对其进行编辑

选项代码:

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

这是选择选择框后价格更新的区域:

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

我使用cURL将表单发布到前面提到的URL,使用以下PHP代码:

$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);

该网站只是回复,好像没有发布帖子。无论我输入什么,都会选择第一个选项。这怎么可能?显然这里有些不对劲。是因为他们处理后期数据的方式我在这里遗漏了什么吗?

3 个答案:

答案 0 :(得分:4)

此网站使用JavaScript获取此信息。

如果您使用Firefox(或Chrome)调试工具,则可以在更改表单值时检查网络活动。

您会看到对http://nl.myprotein.com/variations.json?productId=10530943

的POST通话

您必须使用好的参数来卷曲此网址。请参见下面的屏幕截图:

集管 enter image description here

Post Params enter image description here

结果 enter image description here

答案 1 :(得分:3)

这是我的浏览器发送的请求,用于获取有关价格的信息。

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

我删除了一些特定于浏览器的标头。如果您使用获得的信息,您应该能够自己调用POST请求。表单数据位于请求的最后一行。

服务器返回了JSON:

{"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"}]}]}

有关所选产品价格的信息位于pricerrp变量中(价格带有货币标记)。

答案 2 :(得分:0)

尝试此代码,您将获得价格:

$get_price = explode('<span itemprop="price">', $contents);
$the_price = explode('</span>', $get_price[1]);

echo $the_price[0];

只需将代码放在$contents = curl_exec($c);

之后

另外两个答案很好,并且获取了json数据,这也是一个很好的解决方案。但这只是在您想要将价格作为参考问题的时候。