繁体   English   中英

PHP试图获取非对象nodeValue的属性

[英]PHP Trying to get property of non-object nodeValue

我正在尝试扫描所有 URL(即从http://0.0.0.1http://255.255.255.255 )并获取所有标题和描述,并将它们存储在数据库中。 我知道这是错误的方法,但我无法找到更好的解决方案。 以下代码作为 Cron Job 每 5 分钟执行一次。

扫描.php

#!/usr/bin/php
<?php
error_reporting(E_ALL);
$db_host = "localhost";
$db_username = "root";
$db_password = "*****"; //Connection works fine, hidden for security purpose
$db_name = "lab";
$conn = new mysqli($db_host, $db_username, $db_password, $db_name);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
function file_get_contents_curl($url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}
$sql="SELECT wid FROM websites";
$result=$conn->query($sql);
$num_rows=$result->num_rows;
$nrs=4228250625-$num_rows; //255*255*255*255, going in reverse order
$url_4=$nrs%255;
$nrs=$nrs/255;
$url_3=$nrs%255;
$nrs=$nrs/255;
$url_2=$nrs%255;
$nrs=$nrs/255;
$url_1=$nrs%255;
for($i=1;$i<=5;$i++){
    $url=$url_1.".".$url_2.".".$url_3.".".($url_4-$i);
    try{
    $html = file_get_contents_curl("http://".$url);
    $doc = new DOMDocument();
    @$doc->loadHTML($html);
    $nodes = $doc->getElementsByTagName('title');

    //get and display what you need:
    $title = 'Undefined';
    try {
        if($nodes){
            $title = $nodes->item(0)->nodeValue; //#45 This is where error lies
        }
    }
    catch (Exception $e) {
    }
    $description='';

    $metas = $doc->getElementsByTagName('meta');

    for ($i = 0; $i < $metas->length; $i++)
    {
        $meta = $metas->item($i);
        if($meta->getAttribute('name') == 'description')
            $description = $meta->getAttribute('content');
    }
    $wtime=time();
    $sql="INSERT INTO websites (`url`,`title`,`description`,`wtime`) VALUES ('$url','$title','$description','$wtime')";
    mysqli_query($conn,$sql);
    }
    catch(Exception $e){

    }
}

?>

我在日志文件中收到以下错误:

[17-Nov-2016 06:22:09 Etc/GMT] PHP Notice:  Trying to get property of non-object in /home/roboca6g/public_html/lab/scan.php on line 48
[17-Nov-2016 06:22:10 Etc/GMT] PHP Notice:  Trying to get property of non-object in /home/roboca6g/public_html/lab/scan.php on line 48
[17-Nov-2016 06:22:11 Etc/GMT] PHP Notice:  Trying to get property of non-object in /home/roboca6g/public_html/lab/scan.php on line 48

请帮我解决这个问题,如果可能的话,建议更好的方法来执行类似的任务。

尝试检查实际上是您使用的元素的对象。 尝试:

if(is_object($nodes)){
   //do your stuff
}

基于@winston86 的回答:

if(is_object($nodes ->item(0))) {
$title = $nodes->item(0)->nodeValue; 
}

在我的情况下工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM