phpcms对接阿里云的万维中文分词接口
替换api/get_keywords.php文件内容,代码如下:
<?php
$number = intval($_GET['number']);
$title = $_POST['data'];
echo get_keywords($title, $number);
function get_keywords($title, $number = 3)
{
$host = "http://showapifc.market.alicloudapi.com";
$path = "/sepWord";
$method = "POST";
$appcode = "您自己的appcode";
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
//根据API的要求,定义相对应的Content-Type
array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
$querys = "";
$bodys = "text=".$title;
$url = $host . $path;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
if (1 == strpos("$".$host, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
$data = curl_exec($curl);
$data = json_decode($data,true);
$datas = $data['showapi_res_body']['list'];
$new_data = array_slice($datas, 0, $number);
foreach ($new_data as $v) {
$keywords[] = $v;
}
return implode(',', $keywords);
}
?>
至此结束。