src/Graby.php
private function extractOpenGraph($html, $baseUrl)
    {
        if ('' === trim($html)) {
            return [];
        }

        libxml_use_internal_errors(true);

        $doc = new \DomDocument();
        $doc->loadHTML($html);

        libxml_use_internal_errors(false);

        $xpath = new \DOMXPath($doc);
        $query = '//*/meta[starts-with(@property, \'og:\')]';
        $metas = $xpath->query($query);

        $rmetas = [];
        foreach ($metas as $meta) {
            $property = str_replace(':', '_', $meta->getAttribute('property'));

            if ('og_image' === $property) {
                // avoid image data:uri to avoid sending too much data
                // also, take the first og:image which is usually the best one
                if (0 === stripos($meta->getAttribute('content'), 'data:image') || !empty($rmetas[$property])) {
                    continue;
                }

                $rmetas[$property] = $this->makeAbsoluteStr($baseUrl, $meta->getAttribute('content'));

                continue;
            }

            $rmetas[$property] = $meta->getAttribute('content');
        }

        return $rmetas;
    }
private function extractOpenGraph($html, $baseUrl)
    {
        if ('' === trim($html)) {
            return [];
        }

        libxml_use_internal_errors(true);

        $doc = new \DomDocument();
        $doc->loadHTML($html);

        libxml_use_internal_errors(false);

        $xpath = new \DOMXPath($doc);
        $query = '//*/meta[starts-with(@property, \'og:\')]';
        $metas = $xpath->query($query);

        $rmetas = [];
        foreach ($metas as $meta) {
            $property = str_replace(':', '_', $meta->getAttribute('property'));

            if ('og_image' === $property) {
                // avoid image data:uri to avoid sending too much data
                // also, take the first og:image which is usually the best one
                if (0 === stripos($meta->getAttribute('content'), 'data:image') || !empty($rmetas[$property])) {
                    continue;
                }

                $rmetas[$property] = $this->makeAbsoluteStr($baseUrl, $meta->getAttribute('content'));

                continue;
            }

            $rmetas[$property] = $meta->getAttribute('content');
        }

        return $rmetas;
    }
private function extractOpenGraph ( $ html , $ baseUrl ) { if ( '' === trim ( $ html ) ) { return [ ] ; } libxml_use_internal_errors ( true ) ; $ doc = new \ DomDocument ( ) ; $ doc -> loadHTML ( $ html ) ; libxml_use_internal_errors ( false ) ; $ xpath = new \ DOMXPath ( $ doc ) ; $ query = '//*/meta[starts-with(@property, \'og:\')]' ; $ metas = $ xpath -> query ( $ query ) ; $ rmetas = [ ] ; foreach ( $ metas as $ meta ) { $ property = str_replace ( ':' , '_' , $ meta -> getAttribute ( 'property' ) ) ; if ( 'og_image' === $ property ) { // avoid image data:uri to avoid sending too much data // also, take the first og:image which is usually the best one if ( 0 === stripos ( $ meta -> getAttribute ( 'content' ) , 'data:image' ) || ! empty ( $ rmetas [ $ property ] ) ) { continue ; } $ rmetas [ $ property ] = $ this -> makeAbsoluteStr ( $ baseUrl , $ meta -> getAttribute ( 'content' ) ) ; continue ; } $ rmetas [ $ property ] = $ meta -> getAttribute ( 'content' ) ; } return $ rmetas ; }
Extract OpenGraph data from the response.

@param string $html
@param string $baseUrl Used to make the og:image absolute

@return array

@see http://stackoverflow.com/a/7454737/569101
Extract OpenGraph data from the response .