0

I have had a search around on this but i could not understand the answers . I'm trying to create a web crawler using PHP referencing howCode tutorials

My code is :

function get_details($url)
{
    $options= array('http'=>array('method'=>"GET",'headers'=>"User-Agent: howBot/0.1\n"));

    $context = stream_context_create($options);

    $doc = new DOMDocument();

    @$doc->loadHTML(@file_get_contents($url, false , $context));

    $title = $doc->getElementsByTagName("title");

    $title = $title->item(0)->nodeValue;

    $description = "";

    $keywords = "";

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

    for($i=0;$i < $metas->length;$i++)
    {
        $meta =$metas->item($i);

        if($meta->getAttribute("name") == strtolower("description"))
        {
          $description = $meta->getAttribute("content");
        }

        if($meta->getAttribute("name") == strtolower("keywords"))
        {
          $keywords = $meta->getAttribute("content");
        }
    }

    return '{"Title":"'.$title.'","Description":"'.substr_replace("\n","",$description).'","KeyWords":"'.$keywords.'"}';

}

the error produced by the code snippet is

{"Title":"Google","Description":"","KeyWords":""}PHP Notice: Trying to get property of non-object in /var/www/html/search/index.php on line 18

So can anyone explain how to solve this error please

S.I.
  • 3,250
  • 12
  • 48
  • 77
Akarsh
  • 7
  • 3

0 Answers0