[英]“Trying to get property of non-object” error PHP
I am trying to receive price data from a google search and this code gives me a "Trying to get property of non-object" 我正在尝试从Google搜索中接收价格数据,而这段代码给了我“试图获取非对象的属性”
<?php
$html = file_get_contents('http://www.google.com/#q=ps3&tbm=shop'); //get the html returned from the following url
$glist = array();
$gdoc = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(!empty($html)){ //if any html is actually returned
$gdoc->loadHTML($html);
libxml_clear_errors(); //remove errors for yucky html
$gxpath = new DOMXPath($gdoc);
$name = $gxpath->query('b')->item(0)->nodeValue;
}
echo "<pre>";
print_r($glist);
echo "</pre>";
?>
Any help would be nice! 你能帮忙的话,我会很高兴!
Without any more information, it can be hard to pinpoint where it's coming from. 没有更多信息,很难确定其来源。
Odds are it's coming from this line: 奇怪的是,这是从这条线来的:
$name = $gxpath->query('b')->item(0)->nodeValue;
It could be that the query, item, or nodeValue doesn't exist. 可能是查询,项目或nodeValue不存在。 Try printing out the contents of $gxpath, then $gxpath->query('b'), etc, so you can figure out what's going on. 尝试打印$ gxpath的内容,然后打印$ gxpath-> query('b')等,这样您就可以弄清楚发生了什么。
As a sidenote, you're printing $glist without actually doing anything with it, other than creating an array. 附带说明,您在打印$ glist时实际上不执行任何操作,除了创建数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.