0

为什么我收到以下错误

 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;
      }
    }
 }
4

1 回答 1

0

只需在该行替换$htm为:$dom

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

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

$loadResult = $dom->loadHTML(fetch_url('http://' . rtrim(preg_replace('/^http:\/\//i', '', $blog['blogurl']), '/')));
^^^^^^^^^^^
if ($loadResult) {
    ^^^^^^^^^^^
于 2012-04-12T21:54:50.877 回答