PHP获取网站标题+描述+关键字代码,无需第三方接口

随心笔谈2年前发布 admin
202 0 0

文章摘要

这篇文章介绍了如何在PHP中从HTTPS链接(如`http://www.hu6.cc`)中提取标题、描述和关键词。文章提到,使用`file`函数直接获取HTTPS链接时可能会遇到配置问题,建议用户检查`PHP.ini`中的配置。解决方案包括:1)删除`php.ini`中前导的`;`,重启服务;2)在Linux中安装`openssl`模块;3)使用`curl`函数替代`file`函数。文章还提到,获取`title`和`meta`标签的关键代码如下: ```php header("content-type:text/html; charset=utf8"); $arr = file("http://www.hu6.cc"); if($arr) { foreach($arr as $a) { if(strchr($a, "<title>")) { $a = str_ireplace("<title>", "", $a); $a = str_ireplace("</title>", "", $a); echo $a; // 标题 break; } } } $meta_array = get_meta_tags('http://www.dedecmsok.com'); echo $meta_array["keywords"]; // 关键词 echo $meta_array["description"]; // 描述 ``` 文章还提醒用户在使用`file`函数时确保配置正确,并建议使用`curl`函数作为替代方案。

原生PH获取标题(title)、描述(description)、关键字(keywords)代码,无需第三方API接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
header("content-type:text/html; charset=utf8");
$arr = file("http://www.hu6.cc");
if($arr){
    foreach($arr as $a){
        if(strchr($a,"<title>")){
            $a str_ireplace("<title>","",$a);
            $a str_ireplace("</title>","",$a);
            echo $a;//标题
            break;
        }
    }
}
$meta_array = get_meta_tags('http://www.dedecmsok.com');
echo $meta_array["keywords"];//关键词
echo $meta_array["description"];//描述
?>

如果使用PHP.ini默认配置,用file_get_contents读取https的链接,就会如下错误:

1
Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?

解决方案:

1、php.ini中把extension=php_openssl.dll前面的;删掉,重启服务就可以了。

2、Linux下的PHP,安装openssl模块,安装好了以后就可以访问了。

3、如果不能修改配置,就只换curl函数来替代file_get_contents函数。

© 版权声明

相关文章