从tumblr获取元数据

时间:2014-10-31 23:14:33

标签: php opengraph meta

我想从tumblr博客中将开放图元标记(og:image,og:description)放到我网站上的数据库中。我尝试使用此代码从tubmlr获取meta标签

function file_get_contents_curl($url) {
$ch = curl_init();    
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}
$html = file_get_contents_curl("http://www.example.com");
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description')
    $description = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'keywords')
    $keywords = $meta->getAttribute('content');
}
echo "Title: $title". '<br/><br/>';
echo "Description: $description". '<br/><br/>';
echo "Keywords: $keywords";    

我知道这段代码不会得到开放的图形代码,但问题是它没有得到任何元。该代码适用于exgoogle www.google.com但不适用于tumblr。 我怎么能得到tumblr元? 谢谢!

0 个答案:

没有答案