0

可能重复:
在 PHP 中检索 <title> 的最快方法

假设有一个网站http://www.example.com的 title = "Example" description = "it is an example" 关键字 = "example, question, love php"。

在提交链接时可以获取这些的 php 代码或任何其他代码是什么?

4

2 回答 2

5

如果您想从外部链接获取数据,您想使用 curl 获取页面。

这是一个例子

<?php
$ch = curl_init("http://www.google.nl");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

$title = preg_match('!<title>(.*?)</title>!i', $result, $matches) ? $matches[1] : 'No title found';
$description = preg_match('!<meta name="description" content="(.*?)">!i', $result, $matches) ? $matches[1] : 'No meta description found';
$keywords = preg_match('!<meta name="keywords" content="(.*?)">!i', $result, $matches) ? $matches[1] : 'No meta keywords found';

echo $title . '<br>';
echo $description . '<br>';
echo $keywords . '<br>';
?>

对于 google.nl 这将返回:

Google
No meta description found
No meta keywords found

元描述和关键字可能需要更多调整以供您使用。

小旁注 cURL 默认情况下未安装在 apache 中,因此您可能需要先安装。

这是 cURL php 函数页面: http: //nl3.php.net/manual/en/ref.curl.php

于 2011-12-08T21:34:02.640 回答
0

您的问题太笼统了,但是您需要的工具是 file_get_contents 来获取页面,然后使用正则表达式来查找数据。您可以使用它轻松找到标题,但关键字和描述的确切含义尚不清楚......

于 2011-12-08T21:16:26.823 回答