PHP 获取网站标题的两种方法:curl 与 file_get_contents
使用 PHP 获取网站标题的两种方法:curl 与 file_get_contents
获取网站标题在很多场景下都很有用,例如网页抓取、内容分析等。PHP 中常用的两种获取网站标题的方法是使用 curl 和 file_get_contents 函数。
使用 curl 获取网站标题
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $t_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)');
$ret = curl_exec($ch);
curl_close($ch);
preg_match('/<title>(.*)<\/title>/i', $ret, $title);
$title = str_replace(array('\r\n', '\r', '\n', ',', ' '), '', $title[1]);
代码解释:
- 初始化一个 curl 对象。
- 设置请求 URL。
- 设置返回结果为字符串。
- 关闭 SSL 验证。
- 允许重定向。
- 设置 User-Agent。
- 执行 curl 请求并获取响应。
- 关闭 curl 对象。
- 使用正则表达式匹配
<title>标签中的内容。 - 移除标题中的换行符、逗号和空格。
使用 file_get_contents 获取网站标题
$title = file_get_contents($t_url);
$title = preg_replace('/<title>(.*?)<\/title>/is', '$1', $title);
$title = str_replace(array('\r\n', '\r', '\n', ',', ' '), '', $title);
代码解释:
- 使用
file_get_contents获取网页内容。 - 使用正则表达式匹配
<title>标签中的内容。 - 移除标题中的换行符、逗号和空格。
两种方法的比较
curl更加灵活,可以设置更多请求选项,例如代理、超时等。file_get_contents更简单易用,适合简单的网页内容获取。
选择哪种方法取决于具体的需求。
原文地址: https://www.cveoy.top/t/topic/o7Q9 著作权归作者所有。请勿转载和采集!