ArgusManager.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Qiniu\Storage;
  3. use Qiniu\Auth;
  4. use Qiniu\Config;
  5. use Qiniu\Zone;
  6. use Qiniu\Http\Client;
  7. use Qiniu\Http\Error;
  8. use Qiniu\Http\Proxy;
  9. /**
  10. * 主要涉及了内容审核接口的实现,具体的接口规格可以参考
  11. *
  12. * @link https://developer.qiniu.com/censor/api/5620/video-censor
  13. */
  14. final class ArgusManager
  15. {
  16. private $auth;
  17. private $config;
  18. private $proxy;
  19. public function __construct(
  20. Auth $auth,
  21. Config $config = null,
  22. $proxy = null,
  23. $proxy_auth = null,
  24. $proxy_user_password = null
  25. ) {
  26. $this->auth = $auth;
  27. if ($config == null) {
  28. $this->config = new Config();
  29. } else {
  30. $this->config = $config;
  31. }
  32. $this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
  33. }
  34. /**
  35. * 视频审核
  36. *
  37. * @param string $body body信息
  38. *
  39. * @return array 成功返回NULL,失败返回对象Qiniu\Http\Error
  40. * @link https://developer.qiniu.com/censor/api/5620/video-censor
  41. */
  42. public function censorVideo($body)
  43. {
  44. $path = '/v3/video/censor';
  45. return $this->arPost($path, $body);
  46. }
  47. /**
  48. * 图片审核
  49. *
  50. * @param string $body
  51. *
  52. * @return array 成功返回NULL,失败返回对象Qiniu\Http\Error
  53. * @link https://developer.qiniu.com/censor/api/5588/image-censor
  54. */
  55. public function censorImage($body)
  56. {
  57. $path = '/v3/image/censor';
  58. return $this->arPost($path, $body);
  59. }
  60. /**
  61. * 查询视频审核结果
  62. *
  63. * @param string $jobid 任务ID
  64. * @return array
  65. * @link https://developer.qiniu.com/censor/api/5620/video-censor
  66. */
  67. public function censorStatus($jobid)
  68. {
  69. $scheme = "http://";
  70. if ($this->config->useHTTPS === true) {
  71. $scheme = "https://";
  72. }
  73. $url = $scheme . Config::ARGUS_HOST . "/v3/jobs/video/$jobid";
  74. $response = $this->get($url);
  75. if (!$response->ok()) {
  76. print("statusCode: " . $response->statusCode);
  77. return array(null, new Error($url, $response));
  78. }
  79. return array($response->json(), null);
  80. }
  81. private function getArHost()
  82. {
  83. $scheme = "http://";
  84. if ($this->config->useHTTPS === true) {
  85. $scheme = "https://";
  86. }
  87. return $scheme . Config::ARGUS_HOST;
  88. }
  89. private function arPost($path, $body = null)
  90. {
  91. $url = $this->getArHost() . $path;
  92. return $this->post($url, $body);
  93. }
  94. private function get($url)
  95. {
  96. $headers = $this->auth->authorizationV2($url, 'GET');
  97. return Client::get($url, $headers, $this->proxy->makeReqOpt());
  98. }
  99. private function post($url, $body)
  100. {
  101. $headers = $this->auth->authorizationV2($url, 'POST', $body, 'application/json');
  102. $headers['Content-Type'] = 'application/json';
  103. $ret = Client::post($url, $body, $headers, $this->proxy->makeReqOpt());
  104. if (!$ret->ok()) {
  105. print("statusCode: " . $ret->statusCode);
  106. return array(null, new Error($url, $ret));
  107. }
  108. $r = ($ret->body === null) ? array() : $ret->json();
  109. if (strstr($url, "video")) {
  110. $jobid = $r['job'];
  111. return array($jobid, null);
  112. }
  113. return array($r, null);
  114. }
  115. }