HTMLからFacebookのメタタグを取得しようとしています。
私は単純なhtmldomを使用して、サイトからすべてのhtmlデータを取得しています。preg_replaceを試してみましたが、うまくいきませんでした。
たとえば、このfbメタタグのコンテンツを取得したいと思います。
<meta content="IMAGE URL" property="og:image" />
誰かが助けてくれることを願っています!:-)
HTMLからFacebookのメタタグを取得しようとしています。
私は単純なhtmldomを使用して、サイトからすべてのhtmlデータを取得しています。preg_replaceを試してみましたが、うまくいきませんでした。
たとえば、このfbメタタグのコンテンツを取得したいと思います。
<meta content="IMAGE URL" property="og:image" />
誰かが助けてくれることを願っています!:-)
get_meta_tags()を使用することを提案しようとしましたが、(私にとっては)機能しないようです:s
<?php
$tags = get_meta_tags('http://www.example.com/');
echo $tags['og:image'];
?>
しかし、とにかくDOMDocumentを使用することをお勧めします。
<?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;
?>
それが役に立てば幸い
この方法に従って、fabcebookオープングラフタグのキーペア配列を取得します。
$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];
}