I am trying to get the meta description/keywords of google.com, but I end up with a null array.
<?php
include "simple_html_dom.php";
$url = isset($_POST['url']) ? $_POST['url'] : ''; // this would be http://www.google.com
if(!empty($url) && @file_get_contents($url) == true) {
$html = new simple_html_dom();
$html->load_file($url); //put url or filename in place of xxx
$title = $html->find('title', 0)->plaintext;
//echo $title;
$descr = $html->find("meta[name='description']", 0);
var_dump($descr); // NULL
}
?>
the $title
is being get ok, but the description is a problem and don't understand why.
I also tried
$descr = $html->find("meta[name='description']", 0)->content;
results in Notice: Trying to get property of non-object
or
$descr = $html->find("meta[name='description']", 0)->attr('content');
results in Fatal error: Call to a member function attr() on a non-object
or
$descr = $html->find("meta[name='description']", 0)->getAttribute('content');
results in Fatal error: Call to a member function getAttribute() on a non-object
.
All those errors I believe that they are because meta description cannot be found, despite the fact if you turn on to view source code on Google.com you will see that is the first thing you see after head tag Please help me on this I am a noob in Simple HTML DOM. It's much appreciated.