最近用PHPCMSX部署了一些项目,可是PHPCMSX也面临了部分插件无法使用的问题,就拿常用的关键词自动分词这里,提供分词的HTTPS是无法访问了,导致分词结果是空。于是乎就想到自己改一个出来,翻看代码发现其实自己写一个也不难。步骤分为一下几步:
一、添加新方法:
找到程序位置:“api/get_keywords.php”
0, 'data'=>'', 'msg'=>'未添加百度分词系统变量');
//exit(json_encode($data));
}
$client = new AipNlp($cfg_bdqc_appid, $cfg_bdqc_apikey, $cfg_bdqc_secretkey);
$lexer = $client->lexer($words);
$tagstr = '';
if(isset($lexer['error_code'])) {
$data = array('code'=>0, 'data'=>'', 'msg'=>"请检查百度分词API设置:".$lexer['error_msg']);
}
if(isset($lexer['items'])) {
$items = $lexer['items'];
$qcnum = $cfg_bdqc_qcnum ? $cfg_bdqc_qcnum : rand(1,count($items));
$itemcount = count($items) > $qcnum ? $qcnum : count($items);
$result = array_rand($items, $itemcount == 0 ? 1 : $itemcount);
$resultstr = array();
if(is_array($result)) {
foreach($result as $k => $v) {
$resultstr[] = $items[$v]['item'];
}
$tagstr = implode(',', $resultstr);
} else {
$tagstr = $items[$result]['item'];
}
}
$data = array('code'=>1, 'data'=>$tagstr, 'msg'=>'分词成功');
//exit(json_encode($data));
if ($data && $data['data']) {
return trim($data['data'], ',');
}
break;
case '2':
$XAppid = pc_base::load_config('system', 'xunfei_appid');
$Apikey = pc_base::load_config('system', 'xunfei_apikey');
$fix = 0; //如果错误日志提示【time out|ilegal X-CurTime】,需要把$fix变量改为 100 、200、300、等等,按实际情况调试,只要是数字都行
$XParam = base64_encode(json_encode(array(
"type"=>"dependent",
)));
$XCurTime = SYS_TIME - $fix;
$XCheckSum = md5($Apikey.$XCurTime.$XParam);
$headers = array();
$headers[] = 'X-CurTime:'.$XCurTime;
$headers[] = 'X-Param:'.$XParam;
$headers[] = 'X-Appid:'.$XAppid;
$headers[] = 'X-CheckSum:'.$XCheckSum;
$headers[] = 'Content-Type:application/x-www-form-urlencoded; charset=utf-8';
$rt = json_decode(file_get_contents("https://ltpapi.xfyun.cn/v1/ke", false, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $headers,
'content' => http_build_query(array(
'text' => $words,
)),
'timeout' => 15*60
)
))), true);
if (!$rt) {
//return '接口请求失败';
} elseif ($rt['code']) {
//return '接口请求错误: '.$rt['desc'];
} else {
$msg = '';
foreach ($rt['data']['ke'] as $t) {
$msg.= ','.$t['word'];
}
return trim($msg, ',');
}
return '';
break;
default:
$data = trim(strip_tags($words));
if(empty($words)) return '';
$http = pc_base::load_sys_class('http');
if(CHARSET != 'utf-8') {
$data = iconv('utf-8', CHARSET, $data);
}
$number = pc_base::load_config('system', 'baidu_qcnum') ? pc_base::load_config('system', 'baidu_qcnum') : 4;
$number -= 1;
$http->post(API_URL_GET_KEYWORDS, array('siteurl'=>SITE_URL, 'charset'=>CHARSET, 'data'=>$words, 'number'=>$number));
if($http->is_ok()) {
if(CHARSET != 'utf-8') {
return iconv('gbk','utf-8',$http->get_data());
} else {
return $http->get_data();
}
}
return '';
case '3':
$get_url = 'http://fenci.exv5.com/api_fenci.html?str=' . urlencode($words);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $get_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
]
]);
$response = curl_exec($ch);
curl_close($ch);
$res = preg_replace('/\s+/', '', $response);
return $res;
}
}
?>替换成以上内容,其中case '3'是新增接口,如果有自己的接口或其他网站接口,可以直接在这里修改成自己的接口,如果需要但对为接口设置appkey,需要在系统文件中增加对应接口的配置appkey。
系统配置appkey位置:“caches/configs/system.php”
//关键词提取接口配置 'cwsapi' => '3', //1讯飞,2百度,3南方海豹 'xunfei_appid' => '', //讯飞开放平台 APPID 'xunfei_apikey' => '',//讯飞开放平台 APIKey 'baidu_appid' => '', //百度关键词提取 APPID 'baidu_apikey' => '', //百度关键词提取 APIKey 'baidu_secretkey' => '', //百度关键词提取 Secret Key 'baidu_qcnum' => '4', //百度关键词提取 百度分词数量
在上面这个位置自行添加,名称请自定义不要重复。
二、修改后台接口选择:
后台系统配置页面:“phpcms/modules/admin/templates/system.tpl.php",搜索“PHPCMSX接口”,在这段上方粘贴如下代码:
南方海豹接口: <input type="radio" name="setconfig[cwsapi]" value="3"> 南方海豹 智能分词 [推荐:永久免费/更加人性化] PHPCMSX接口: <input type="radio" name="setconfig[cwsapi]" value="0"> PHPCMSX 智能分词 [推荐:永久免费/更加人性化]
粘贴后保存即可,保存后进入测试接口环节。
三、测试结果:
新建一篇文章,使用浏览器的开发者模式,选择“Network”模式后,在标题处随意输入一些词语(乱输入可能无效),然后点击关键词。在查看提交的结果。最后看看我的图。

在此就全部结束了,希望你们能够成功!
相关推荐
Copyright © 2019-2024 贵州海豹文化传播有限公司 版权所有 · 黔ICP备2022002053号-1