Skip to content

Instantly share code, notes, and snippets.

@tutweb
Last active May 27, 2019 10:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tutweb/8ae422dbb21370ab0715 to your computer and use it in GitHub Desktop.
Save tutweb/8ae422dbb21370ab0715 to your computer and use it in GitHub Desktop.
Get Open Graph Meta Tag Using PHP
<?php
function fetch_og($url)
{
$data = file_get_contents($url);
$dom = new DomDocument;
@$dom->loadHTML($data);
$xpath = new DOMXPath($dom);
# query metatags dengan prefix og
$metas = $xpath->query('//*/meta[starts-with(@property, \'og:\')]');
$og = array();
foreach($metas as $meta){
# ambil nama properti tanpa menyertakan og
$property = str_replace('og:', '', $meta->getAttribute('property'));
# ambil konten dari properti tersebut
$content = $meta->getAttribute('content');
$og[$property] = $content;
}
return $og;
}
# Cara penggunaan, cukup masukan url
$og = fetch_og('http://www.jurnalweb.com/5-tips-membangun-website-sendiri/');
echo "<pre>";
print_r($og);
echo "</pre>";
echo "<hr>";
echo $og['title'];
echo "<hr>";
echo "<h1>Data Facebook Jurnalweb.com</h1>";
echo "<h3>Link: <small><small>http://www.jurnalweb.com/5-tips-membangun-website-sendiri/</small></small></h3>";
foreach ($og as $key => $value) {
echo "<strong>$key:</strong> ".$value;
echo "<br>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment