阿里云文本垃圾内容检测接口调用
- 开发技术
- 2020-04-27
- 1568
- 0
安装扩展包composer require alibabacloud/sdk
类文件(laravel写法)
<?php
namespace App\Services;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class AlibabaCloudServices
{
/**
* 文本垃圾内容检测
* @param $text
* @throws ClientException
*/
public function greenTextScan($text)
{
AlibabaCloud::accessKeyClient(config('ali.access_key_id'), config('ali.access_key_secret'))
->regionId('cn-beijing')
->asDefaultClient();
try {
$body = array(
'scenes' => 'antispam',
'tasks' => [
'content' => $text
]
);
$result = AlibabaCloud::roa()
->product('Green')
// ->scheme('https') // https | http
->version('2018-05-09')
->pathPattern('/green/text/scan')
->method('POST')
->options([
'query' => [
]
])
->body(json_encode($body))
->request();
print_r($result->toArray());
} catch (ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}
}
}