阿里云文本垃圾内容检测接口调用

PHP  

安装扩展包
composer require alibabacloud/sdk

类文件(laravel写法)

  1. <?php
  2. namespace App\Services;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. class AlibabaCloudServices
  7. {
  8. /**
  9. * 文本垃圾内容检测
  10. * @param $text
  11. * @throws ClientException
  12. */
  13. public function greenTextScan($text)
  14. {
  15. AlibabaCloud::accessKeyClient(config('ali.access_key_id'), config('ali.access_key_secret'))
  16. ->regionId('cn-beijing')
  17. ->asDefaultClient();
  18. try {
  19. $body = array(
  20. 'scenes' => 'antispam',
  21. 'tasks' => [
  22. 'content' => $text
  23. ]
  24. );
  25. $result = AlibabaCloud::roa()
  26. ->product('Green')
  27. // ->scheme('https') // https | http
  28. ->version('2018-05-09')
  29. ->pathPattern('/green/text/scan')
  30. ->method('POST')
  31. ->options([
  32. 'query' => [
  33. ]
  34. ])
  35. ->body(json_encode($body))
  36. ->request();
  37. print_r($result->toArray());
  38. } catch (ClientException $e) {
  39. echo $e->getErrorMessage() . PHP_EOL;
  40. } catch (ServerException $e) {
  41. echo $e->getErrorMessage() . PHP_EOL;
  42. }
  43. }
  44. }


评论 0

发表评论

Top