DEV Community

Wallace Maxters
Wallace Maxters

Posted on

How to get Open Graph Protocol information for a website with PHP?


function get_og_tags($url)
{
    $dom = new DOMDocument('1.0', 'UTF-8');
    @$dom->loadHTMLFile($url);

    $result = [];

    foreach ($dom->getElementsByTagName('meta') as $node) {

        $prop = $node->getAttribute('property');

        if (substr($prop, 0, 3) !== 'og:') continue;

        $result[$prop] = $node->getAttribute('content');
    }

    return $result;
}


$ogs = get_og_tags('https://wallacemaxters.com.br');

print_r($ogs);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)