Skip to content

Instantly share code, notes, and snippets.

@joeydenbraven
Created November 19, 2015 08:23
Show Gist options
  • Save joeydenbraven/4951de9ac83b1ccb1cad to your computer and use it in GitHub Desktop.
Save joeydenbraven/4951de9ac83b1ccb1cad to your computer and use it in GitHub Desktop.
A way to get meta data from external url via php including opengraph items, so you can use it on your site.
<?php
// Url
$link = 'http://www.google.com';
//Get all meta tags and loop through them
if($link!=''){
$sites_html = file_get_contents($link);
$html = new DOMDocument();
@$html->loadHTML($sites_html);
$meta_og_img = null;
foreach($html->getElementsByTagName('meta') as $meta) {
if($meta->getAttribute('name')=='title'){
$meta_title = $meta->getAttribute('content');
}
if($meta->getAttribute('name')=='author'){
$meta_author = $meta->getAttribute('content');
}
if($meta->getAttribute('name')=='description'){
$meta_description = $meta->getAttribute('content');
}
//Facebook
if($meta->getAttribute('property')=='og:title'){
$meta_og_title = $meta->getAttribute('content');
}
if($meta->getAttribute('property')=='og:site_name'){
$meta_og_sitename = $meta->getAttribute('content');
}
if($meta->getAttribute('property')=='og:image'){
$meta_og_img = $meta->getAttribute('content');
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment