PHP - Errors with DOmdocument


PHP - Errors with DOmdocument

为什么我收到以下错误

 Call to a member function getElementsByTagName() on a non-object

以下行发生错误:

  $title = $htm->getElementsByTagName('title');

当我运行以下代码时:

  $dom = new DOMDocument();
  foreach ($all as $blog) {
    sleep(1);
    $htm = $dom->loadHTML(fetch_url('http://' . rtrim(preg_replace('/^http:'/'//i', '', $blog['blogurl']), '/')));
    if ($htm) {
      //check TITLE
      $title = $htm->getElementsByTagName('title');
      $title = $title->item(0)->nodeValue;
      if (preg_match('/private/i', $title)) {
        private_blog($blog['id']);
        $title = null;
        unset($title);
        gc_collect_cycles();
        continue;
      }
    }
 }

只需将该行中的$htm替换为$dom

$title = $dom->getElementsByTagName('title');
         ^^^^

要进一步改进,请重命名上面另一行中的$htm变量:

$loadResult = $dom->loadHTML(fetch_url('http://' . rtrim(preg_replace('/^http:'/'//i', '', $blog['blogurl']), '/')));
^^^^^^^^^^^
if ($loadResult) {
    ^^^^^^^^^^^