【问题标题】:PHP Trying to get property of non-object nodeValuePHP试图获取非对象nodeValue的属性
【发布时间】:2019-06-11 19:29:22
【问题描述】:

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

scan.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

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

【问题讨论】:

  • 我们应该数行数来找出第​​ 48 行在哪里?
  • 我会更新错误行
  • 你能在 $doc = new DOMDocument(); 之后立即转储 $doc;
  • @wuno 我删除了循环并尝试了var_dump($doc);,它返回了一些输出。但是当我添加回循环时,它给了我 500 错误。现在我无法让它恢复工作。
  • 如果它返回了一个输出,那么它是因为它正在工作。听起来您只是没有正确解析数据。请看我的回答。您需要将其解析为字符串。

标签: php dom curl cron


【解决方案1】:

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

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

【讨论】:

  • 抱歉,我无法验证您的答案是否有效,因为我收到 500 错误。
【解决方案2】:

基于@winston86 的回答:

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

在我的情况下工作。

【讨论】:

    猜你喜欢
    • 2017-08-21
    • 1970-01-01
    • 2011-08-02
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多