Skip to content

Instantly share code, notes, and snippets.

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

What would you like to do?
Get Open Graph Meta Tag Using PHP
function fetch_og($url)
$data = file_get_contents($url);
$dom = new DomDocument;
$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('');
echo "<pre>";
echo "</pre>";
echo "<hr>";
echo $og['title'];
echo "<hr>";
echo "<h1>Data Facebook</h1>";
echo "<h3>Link: <small><small></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.