Last active
May 27, 2019 10:25
-
-
Save tutweb/8ae422dbb21370ab0715 to your computer and use it in GitHub Desktop.
Get Open Graph Meta Tag Using PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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