一个简单的函数,用于了解如何检索og:tags、标题和描述信息,请根据自己的需求进行适当修改。
function read_og_tags_as_json($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);
$HTML_DOCUMENT = curl_exec($ch);
curl_close($ch);
$doc = new DOMDocument();
$doc->loadHTML($HTML_DOCUMENT);
$res['title'] = $doc->getElementsByTagName('title')->item(0)->nodeValue;
foreach( $doc->getElementsByTagName('meta') as $m ){
if( $m->getAttribute('property') ){
$prop = $m->getAttribute('property');
if( preg_match("/og:/i", $prop) ){
$res['og_tags'][] =
array( 'property' => $m->getAttribute('property'),
'content' => $m->getAttribute('content') );
}
}
if( $m->getAttribute('name') == 'description' ){
$res['description'] = $m->getAttribute('content');
}
}
echo json_encode($res, JSON_PRETTY_PRINT |
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
返回此页面的结果(可能有更多信息):
{
"title": "php - Getting title and meta tags from external website - Stack Overflow",
"og_tags": [
{
"property": "og:type",
"content": "website"
},
{
"property": "og:url",
"content": "https://dev59.com/iHA65IYBdhLWcg3wogOe"
},
{
"property": "og:site_name",
"content": "Stack Overflow"
},
{
"property": "og:image",
"content": "https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon@2.png?v=73d79a89bded"
},
{
"property": "og:title",
"content": "Getting title and meta tags from external website"
},
{
"property": "og:description",
"content": "I want to try figure out how to get the\n\n<title>A common title</title>\n<meta name=\"keywords\" content=\"Keywords blabla\" />\n<meta name=\"description\" content=\"This is the descript..."
}
]
}
$meta->getAttribute()
周围添加strtolower
!!有时它们的首字母是大写的。 - user993683$nodes = $doc->getElementsByTagName('title');
返回结果的长度,当标题缺失时会抛出警告。因此,只需将下一行替换为if ($nodes->length>0) { $title = $nodes->item(0)->nodeValue; }
即可。 - Penny