Get Facebook meta tags with PHP

前端 未结 2 920
野性不改
野性不改 2020-11-29 10:55

I\'m trying to get Facebook\'s meta tags from my HTML.

I\'m using simple html dom to get all html data from the site. I\'ve tried with preg_replace, but without luck

相关标签:
2条回答
  • 2020-11-29 11:22

    I Was going to suggest to use get_meta_tags() but it seems to not work (for me) :s

    <?php
    $tags = get_meta_tags('http://www.example.com/');
    echo $tags['og:image'];
    ?>
    

    But I would rather suggest using DOMDocument anyways:

    <?php
    $sites_html = file_get_contents('http://example.com');
    
    $html = new DOMDocument();
    @$html->loadHTML($sites_html);
    $meta_og_img = null;
    //Get all meta tags and loop through them.
    foreach($html->getElementsByTagName('meta') as $meta) {
        //If the property attribute of the meta tag is og:image
        if($meta->getAttribute('property')=='og:image'){ 
            //Assign the value from content attribute to $meta_og_img
            $meta_og_img = $meta->getAttribute('content');
        }
    }
    echo $meta_og_img;
    ?>
    

    Hope it helps

    0 讨论(0)
  • 2020-11-29 11:34

    As per this method you will get key pair array of fabcebook open graph tags.

     $url="http://fbcpictures.in";
     $site_html=  file_get_contents($url);
        $matches=null;
        preg_match_all('~<\s*meta\s+property="(og:[^"]+)"\s+content="([^"]*)~i',     $site_html,$matches);
        $ogtags=array();
        for($i=0;$i<count($matches[1]);$i++)
        {
            $ogtags[$matches[1][$i]]=$matches[2][$i];
        }
    

    Output of facebook open graph tags

    0 讨论(0)
提交回复
热议问题