Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对非对象调用成员函数_Php_Domdocument - Fatal编程技术网

Php 对非对象调用成员函数

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'

我需要创建一个基于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');
        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。非常感谢。