简体   繁体   中英

Get title and meta tags from Facebook page with PHP

How can I get the title and all the meta tags from a Facebook page?

I have this code:

function getMetaData($url){
   // get meta tags
   $meta=get_meta_tags($url);
   // store page
   $page=file_get_contents($url);
   // find where the title CONTENT begins
   $titleStart=strpos($page,'<title>')+7;
   // find how long the title is
   $titleLength=strpos($page,'</title>')-$titleStart;
   // extract title from $page
   $meta['title']=substr($page,$titleStart,$titleLength);
   // return array of data
   return $meta;
}
$tags=getMetaData('https://www.facebook.com/showmeapp?filter=3');
echo 'Title: '.$tags['title'];
echo '<br />';
echo 'Description: '.$tags['description'];
echo '<br />';
echo 'Keywords: '.$tags['keywords']

Example: https://www.facebook.com/showmeapp?filter=3

You really don´t need to deal with the Metatags, just call the Graph API:

$data = file_get_contents('https://graph.facebook.com/bladauhu'); //example page

Works even without an App, for every public page.

我认为网址不正确,请将您的网址更改为https://graph.facebook.com/showmeapp

Use Simple HTML DOM Parser

http://simplehtmldom.sourceforge.net/

Try below code:

  <?php 
  require_once('simple_html_dom.php');

  $page=$html = file_get_html('https://www.facebook.com/showmeapp');

  $title= $html->find('meta[name="title"]',0)->content;
  echo 'Title: '.$title ;
  echo '<br />';

  $description= $html->find('meta[name="description"]',0)->content;

  echo 'Description: '.$description;
  echo '<br />';

  $keywords= $html->find('meta[name="keywords"]',0)->content;
  echo 'Keywords: '.$keywords;
  ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM