// Set url
$url = 'http://edition.cnn.com/2015/04/03/politics/white-house-congress-iran-nuclear-deal/index.html';
// This function is going to go to a url or file and pull all the data that's held in it.
$html = file_get_contents($url);
// This function allows user to disable standard libxml errors and enable user error handling if you are so worried about using @ with warnings
libxml_use_internal_errors(true);
// Load the document
$doc = new DomDocument();
// Set array to hold meta name & value
$data = array();
if (!empty($html)) {
if ($doc->loadHTML($html)) {
$xpath = new DOMXPath($doc);
$query = '//*/meta[starts-with(@property, \'og:\')]';
$metas = $xpath->query($query);
foreach ($metas as $meta) {
// Get name
$property = $meta->getAttribute('property');
// Get value
$content = $meta->getAttribute('content');
// Assign name & value to array
$data[$property] = $content;
}
}
else {
foreach (libxml_get_errors() as $error) {
// handle errors here
}
}
}
print_r($data);
0 Comment(s)