dongnanman9093 2014-05-01 13:07
浏览 54
已采纳

file_get_contents没有获取正确的结果

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

  • 写回答

2条回答 默认 最新

  • dstwfcz1377 2014-05-01 13:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办