cache tag php,获取文章标签tag php demo tp5

获取文章标签tag php demo 小弟在这里献丑了,大神勿喷!

实测可用(thinkphp5 )

这里我是把access_token存储到数据库的表结构

数据库名称自己可以修改

DROP TABLE IF EXISTS `dx_access_token`;

CREATE TABLE `dx_access_token` (

`access_token` char(255) DEFAULT NULL,

`refresh_token` char(255) DEFAULT NULL,

`expires_in` int(10) DEFAULT NULL,

`scope` char(255) DEFAULT NULL,

`session_key` char(255) DEFAULT NULL,

`session_secret` char(255) DEFAULT NULL,

`id` int(11) NOT NULL AUTO_INCREMENT,

`time` int(10) DEFAULT NULL,

`over_time` int(11) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;

先来个获取 access_token 的函数

function GetAccessToken(){

$model = new \think\Db;

$cache = $model::name('AccessToken')->where(['over_time'=>['gt',@time()]])->find();

if (!empty($cache)) {

return $cache['access_token'];

}else{

$url = "https://aip.baidubce.com/oauth/2.0/token";

$data = [

'grant_type' => 'client_credentials',

'client_id' => '你的apiKey',

'client_secret' => '你的secretKey'

];

$refault = _Curl($url,$data);

if (isset($refault['access_token'])) {

$model::name('AccessToken')->insert([

'access_token' => $refault['access_token'],

'refresh_token' => $refault['refresh_token'],

'expires_in' => $refault['expires_in'],

'scope' => $refault['scope'],

'session_key' => $refault['session_key'],

'session_secret'=> $refault['session_secret'],

'time' => time(),

'over_time' => (time()+$refault['expires_in'])

]);

return $refault['access_token'];

}elseif (isset($refault['error'])) {

return $refault['error'];die;

}else{

return '获取失败';

}

}

curl封装函数

function _Curl($url, $data=null,$is_jsondecode=true){

if (empty($url)) {

return false;

}

$a = array("Content-type:application/json");

//初始化curl

$ch = curl_init();

//设置超时

curl_setopt($ch, CURLOPT_TIMEOUT, 60);

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);

curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);

if (!empty($data)){

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);

}

curl_setopt($ch, CURLOPT_HEADER, FALSE);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ch, CURLOPT_HTTPHEADER, $a);

//运行curl,结果以jason形式返回

$res = curl_exec($ch);

curl_close($ch);

//取出返回数据

//$res = file_get_contents($url);

return $data = ($is_jsondecode==true?json_decode($res,true):$res);

}

获取文章标签tag函数

/**

* 文章标签接口

*

* @param string $title - 篇章的标题,最大80字节

* @param string $content - 篇章的正文,最大65535字节

* @param array $options - 可选参数对象,key: value都为string类型

* @description options列表:

* @return array

*/

function get_keywords($title, $content, $options=array()){

$data = array();

$data['title'] = $title;

$data['content'] = $content;

$data = array_merge($data, $options);

//这里就是重点了,先把数组转成json 然后把utf-8转成 gbk

$data = mb_convert_encoding(json_encode($data), 'GBK', 'UTF8');

$keywordUrl = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/keyword?access_token='.GetAccessToken();

//返回数据也需要把gbk 转成utf-8

return mb_convert_encoding(_Curl($keywordUrl, $data,false), 'UTF8', 'GBK');

}

调用实例:

public function hello(){

$str = 'iphone手机出现“白苹果”原因及解决办法,用苹果手机的可以看下';

$str2 = '如果下面的方法还是没有解决你的问题建议来我们门店看下成都市锦江区红星路三段99号银石广场24层01室。在通电的情况下掉进清水,这种情况一不需要拆机处理。尽快断电。用力甩干,但别把机器甩掉,主意要把屏幕内的水甩出来。如果屏幕残留有水滴,干后会有痕迹。^H3 放在台灯,射灯等轻微热源下让水分慢慢散去。';

$data =[

'title' => $str,

'content' => $str2

];

print(get_keywords($str,$str2));

}

有问题欢迎在下方发问,本人博客http://www.xjyili.cn/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值