PHP SDK开发包下载
https://help.aliyun.com/knowledge_detail/50180.html?spm=5176.7850179.2.1.5BZP9q
<?php
namespace Potting;
include_once 'Aliyun/aliyun-php-sdk-core/Config.php';
use Green\Request\V20170112 as Green;
class TextScan{
private static $accessKeyId='';
private static $accessKeySecret='';
static public function text($content){
date_default_timezone_set("PRC");
$iClientProfile = \DefaultProfile::getProfile("cn-shanghai", self::$accessKeyId, self::$accessKeySecret);
\DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new \DefaultAcsClient($iClientProfile);
$request = new Green\TextScanRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
$task1 = array('dataId' => uniqid(),
'content' => $content
);
/**
* 文本垃圾检测: antispam
* 关键词检测: keyword
**/
$request->setContent(json_encode(array("tasks" => array($task1),
"scenes" => array("antispam"))));
try {
$response = $client->getAcsResponse($request);
$result=array();
if(200 == $response->code){
$taskResults = $response->data;
foreach ($taskResults as $taskResult) {
if(200 == $taskResult->code){
$sceneResults = $taskResult->results;
foreach ($sceneResults as $sceneResult) {
$scene = $sceneResult->scene;
$suggestion = $sceneResult->suggestion;
$result=$taskResult;
}
}else{
print_r("task process fail:" + $response->code);
}
}
}else{
print_r("detect not success. code:" + $response->code);
}
$result=$result->results[0];
$data=array();
if($result->label == 'normal'){
$data['code']=true;
$data['label']=$result->label;
}else{
$data['code']=false;
$data['label']=self::getlabel($result->label);
}
return $data;
} catch (Exception $e) {
print_r($e);
}
}
static private function getlabel($label){
switch ($label){
case 'normal':
return '正常文本';
break;
case 'spam':
return '输入的内容含垃圾信息';
break;
case 'ad':
return '输入的内容含广告';
break;
case 'politics':
return '输入的内容含渉政';
break;
case 'terrorism':
return '输入的内容含暴恐';
break;
case 'abuse':
return '输入的内容含辱骂';
break;
case 'porn':
return '输入的内容含色情';
break;
case 'flood':
return '输入的内容含灌水';
break;
case 'contraband':
return '输入的内容含垃违禁';
break;
case 'customized':
return '输入的内容包含敏感词';
break;
default:
return '';
break;
}
}
}