我正在开发脚本来获取PHP5的POO中带有cURL的网站的某些元素,这是我的代码:
class obtain{
protected $url;
private $handler;
protected $response;
function __construct($url){
$this->url = $url;
}
protected function curl(){
$this->handler = curl_init($this->url);
curl_setopt($this->handler, CURLOPT_RETURNTRANSFER, true);
$this->response = curl_exec($this->handler);
curl_close($this->handler);
return $this->response;
}
}
class page extends obtain{
private $reponse;
private $dom;
function __construct(){
parent::__construct('http://www.page.com');
$this->response = parent::curl();
$this->dom = new DOMDocument();
$this->dom = $this->dom->loadHTML($this->response);
var_dump($this->dom->getElementById('contenido-portada'));
}
}
new page();
运行它时出现此错误:
致命错误:在非对象中调用成员函数getElementById()...
为什么?
谢谢!
在这一行:
$this->dom = $this->dom->loadHTML($this->response);
您正在使用loadHTML
;加载HTML 。但是您正在将值分配回$this->dom
。loadHTML
根据布尔值是否有效返回一个布尔值,因此您将覆盖现有对象。
您可能应该执行以下操作:
if (! $this->dom->loadHTML($this->response)) {
// handle error
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句