我正在尝试扫描所有网址(例如,从http://0.0.0.1到http://255.255.255.255)并获取所有标题和说明,并将其存储在数据库中。我知道这是错误的方法,但我无法找到更好的解决方案。以下代码每5分钟作为Cron Job执行。
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
请帮我解决这个问题,如果可能的话,建议更好的方法来执行类似的任务。
答案 0 :(得分:1)
尝试检查实际上是您使用的元素的对象。尝试:
if(is_object($nodes)){
//do your stuff
}
答案 1 :(得分:0)
基于@ winston86答案:
if(is_object($nodes ->item(0))) {
$title = $nodes->item(0)->nodeValue;
}
以我的情况工作。