1

我有这个功能:

        function getTitle(){
    $crawler = new DOM_Crawler($this->url);
            try{
            if ( $title = $crawler->getPage()->getPageTitle()) {
                $this->debug_element("title", $title);
                unset($crawler);
                return $title;
            } else {
                unset($crawler);
                return self::TITLE_FETCH_WARN;
            }
            }
            catch(Exception $e){
                return self::TITLE_FETCH_WARN;
            }
}

我试图阻止爬虫..成为非对象并防止抛出该错误..尝试捕获..那么我怎样才能捕获异常?

致命错误:在第 163 行的 C:\Program Files (x86)\Zend\Apache2\htdocs\backlinks\cron\Backlinks.php 中的非对象上调用成员函数 getPageTitle()

4

1 回答 1

2

尝试

$page = $crawler->getPage();
if ($page && ($title = $page->getPageTitle())) {
于 2012-05-24T07:48:34.083 回答