使用 PHP 从 Facebook 页面获取标题和元标记


Get title and meta tags from Facebook page with PHP

如何从Facebook页面获取标题和所有元标记?

我有这个代码:

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']

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

你真的不需要处理元标记,只需调用图形 API:

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

即使没有应用程序,也适用于每个公共页面。

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

使用简单的 HTML DOM 解析器

http://simplehtmldom.sourceforge.net/

尝试以下代码:

  <?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;
  ?>