从特定链接php获取标题



我正试图从anilink url中获取标题。这个特定的代码适用于MyAnimeList网站,但在AniList网站上,它不断返回"AniList",这就是网站,我相信有问题的网站在使用jquery加载网页后正在更新元标签,然而像facebook和discord这样的网站能够获得系列的标题。但是我的代码不能。

这是我正在使用的代码。例如,这里有一个来自anilist网站的随机url

https://anilist.co/anime/527/Pocket-Monsters/

myfunction(https://anilist.co/anime/527/Pocket-Monsters/)
function myfunction($form_value)
{
$html = file_get_contents_curl($form_value);

//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('property') == 'og:title')
{$title = $meta->getAttribute('content');}
if($meta->getAttribute('property') == 'og:site_name')
$site_name = $meta->getAttribute('content');
}

return $title;

}

andi它回来了。

AniList 

因为这是元标签。

<meta property="og:title" content="Pokémon" data-vue-meta="true">

所以我希望它能返回

Pokémon

我应该使用另一个网站来获得想要的结果吗?

Anilist是页面标记中给出的标题。如果您在浏览器中看到其他内容,请检查应用程序是否使用Javascript覆盖标题。如果是这种情况,那么纯PHP方法对读取页面的最终标题没有帮助。您要么需要在浏览器中运行整个页面并从中读取输出,要么使用正确的API

最新更新