【问题标题】:file_get_contents not fetching the correct resultsfile_get_contents 没有获取正确的结果
【发布时间】:2014-06-17 23:13:39
【问题描述】:

我正在尝试从 play 和 amazon 获取个人项目的价格,但我有 2 个问题。 首先,我有游戏可以工作,但它得到了错误的价格,其次,亚马逊没有得到任何结果。

这是我一直在尝试的代码。

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

然后回显结果:

echo "Play :: New: $newpricep - Used: $usedpricep";
echo "Amazon :: New: $newpricea - Used: $usedpricea";

让你知道发生了什么

$getdata[getlist_item] = "American Pie 5: The Naked Mile";

工作正常。

知道为什么这些不能正常工作吗?

编辑:我刚刚意识到 file_get_contents 中的$getdata[getlist_item] 没有使用该变量,只是按原样打印该变量...为什么这样做???

【问题讨论】:

标签: php regex file-get-contents


【解决方案1】:

您使用的引号不一致!您的开盘价和收盘价都必须相同。

试试这个:

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

就像“.$getdata[getlist_item]”一样。被认为是字符串的一部分,因为您从未关闭您启动的单引号字符串。

【讨论】:

  • 很高兴我能帮上忙 :)
  • @FoxyFish 你真的应该在数组键周围加上引号,比如$getdata['getlist_item']。不带引号的字符串文字是旧 PHP 版本的遗物。另外,根本不需要使用连接,你可以做"http://www.play.com/Search.html?searchstring={$getdata['getlist_item']}&amp;searchsource=0&amp;searchtype=r2alldvd"
  • FoxyFish 所说的非常正确。我错过了,但你真的应该添加它们!编辑:我已经相应地更新了代码
  • 这不是@FoxyFish 说的,我包括了这个名字,所以他被通知了。很好,你已经编辑了你的答案。
  • 对不起,我还是 Stackoverflow 的新手,在工作时回答问题可能会造成混乱:P 但是信用到期...谢谢 Tom Fenech ;)
【解决方案2】:

使用带有正确标题的curl 函数。下面的代码将读取任何网页,然后使用适当的解析器 DOMDocument 或 simpleHTMLDomParser 工具从 html 内容中读取价格

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

【讨论】:

  • 引用你的数组键:$getdata[getlist_item] 应该是$getdata['getlist_item']
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 2017-11-19
  • 2014-05-03
  • 1970-01-01
  • 2014-09-13
  • 2016-09-01
相关资源
最近更新 更多