Skip to content

Instantly share code, notes, and snippets.

@tutweb tutweb/og-php.php
Last active May 27, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.