Php 对非对象调用成员函数
我需要创建一个基于url返回html的函数Php 对非对象调用成员函数,php,domdocument,Php,Domdocument,我需要创建一个基于url返回html的函数 function start() { $url = "http://dostuff.com"; $site = new \DOMDocument(); $site->loadHTML(file_get_contents($url)); //do stuff with it $listview = $site->getElementById('colLeft'
function start()
{
$url = "http://dostuff.com";
$site = new \DOMDocument();
$site->loadHTML(file_get_contents($url));
//do stuff with it
$listview = $site->getElementById('colLeft');
var_dump($this->getValuesOfAttribute($listview,'a','href'));
}
这实际上是可行的,但是我需要在其他几个函数中使用这个功能,所以我也可以用它自己的方法获取内容
public function start()
{
$site = $this->getHTMLByURL("http://dostuff.com");
//do stuff with it
$listview = $site->getElementById('colLeft');
var_dump($this->getValuesOfAttribute($listview,'a','href'));
}
public function getHTMLByURL($url)
{
$site = new \DOMDocument();
return $site->loadHTML(file_get_contents($url));
}
致命错误:调用
非宾语
[文件路径]
对非对象调用成员函数getElementById()
为什么“$site”是非对象?它的值是否与第一个函数的值相同?您的函数
getHTMLByUrl
没有返回您认为的值
public function getHTMLByURL($url)
{
$site = new \DOMDocument();
return $site->loadHTML(file_get_contents($url));
}
它返回loadHTML
调用的布尔结果,而不是对象
有关文档,请参阅
您需要做的是:
public function getHTMLByURL($url)
{
$site = new \DOMDocument();
$site->loadHTML(file_get_contents($url));
return $site;
}
因此,它从LoadHTML返回布尔值,而不是DOMDocument。非常感谢。