简体   繁体   English

使用curl和get_meta_tags()获取元标签信息

[英]getting meta tags info using curl and get_meta_tags()

is there a way to use curl such that you can do something that is equivalent to the get_meta_tags() function in php? 有没有一种方法可以使用curl,使您可以执行与php中的get_meta_tags()函数等效的操作? specifically to get the meta tags of an external site using curl in php with the least amount of overhead possible 专门使用php中的curl获取外部站点的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');
if($meta->getAttribute('language') == 'language');
    $language = $meta->getAttribute('language');
}

echo "Title: $title". '<br/><br/>';
echo "Description: $description". '<br/><br/>';
echo "Keywords: $keywords";

is there a way to use curl such that you can do something that is equivalent to the get_meta_tags() function in php 有没有一种方法可以使用curl,以便您可以执行与php中的get_meta_tags()函数等效的操作

Nope, I don't think so. 不,我不这么认为。

The best way would be to fetch the data, and parse it using a HTML parser . 最好的方法是获取数据,然后使用HTML解析器对其进行解析 Alternatively, there are several regex based approaches in the user contributed notes in the manual. 或者, 手册中用户提供的注释中有几种基于正则表达式的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM