class AliyunGreen
{
private $accessKeyId;
private $accessKeySecret;
private $url = 'https://green.%s.aliyuncs.com%s';
private $api;
private $signatureNonce = '';
public function __construct($accessKeyId, $accessKeySecret, $area, $api, $signatureNonce)
{
$this->accessKeyId = $accessKeyId;
$this->accessKeySecret = $accessKeySecret;
$this->api = $api;
$this->url = \sprintf($this->url, $area, $api);
$this->signatureNonce = $signatureNonce;
}
private function setQueryUrl(string $body, string $clientInfo)
{
$headers = $this->setHeader($body);
$str = "POST" . PHP_EOL;
foreach ($headers as $k => $v) {
if (strpos($k, 'x-acs') === 0) {
$str .= $k . ':' . $v . PHP_EOL;
} else {
$str .= $v . PHP_EOL;
}
}
if (isset($clientInfo) && $clientInfo != '') {
$str .= "$this->api?clientInfo=" . $clientInfo;
} else {
$str .= $this->api;
}
$signature = base64_encode(hash_hmac("sha1", $str, $this->accessKeySecret, true));
$Authorization = ['Authorization' => "acs $this->accessKeyId:$signature"];
$httpHeader = array_merge($headers, $Authorization);
$queryHeader = [];
foreach ($httpHeader as $k => $v) {
$queryHeader[] = $k . ':' . $v;
}
if (isset($clientInfo) && $clientInfo != '') {
$this->url = $this->url . '?clientInfo=' . urlencode($clientInfo);
}
$res = $this->curl($this->url, true, $body, $queryHeader);
return json_decode($res, 1);
}
private function setHeader(string $body): array
{
$headers = [
'Accept' => 'application/json',
'Content-MD5' => base64_encode(md5($body, 1)),
'Content-Type' => 'application/json',
'Date' => gmdate('D, d M Y H:i:s T', time()),
'x-acs-version' => '2018-05-09',
'x-acs-signature-nonce' => $this->signatureNonce == '' ? $this->getSignatureNonce(10) : $this->signatureNonce,
'x-acs-signature-version' => '1.0',
'x-acs-signature-method' => 'HMAC-SHA1',
];
ksort($headers);
return $headers;
}
public function curl(string $url, bool $isPost, $arr, array $header)
{
try {
$ch = curl_init();
if ($ch === false) {
throw new Exception('failed to initialize');
}
if (stripos($url, "https://") !== false) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 0);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (!empty($isPost)) {
if (is_array($arr)) {
$content = http_build_query($arr);
} else {
$content = $arr;
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
}
$sContent = curl_exec($ch);
if ($sContent === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
$aStatus = curl_getinfo($ch);
curl_close($ch);
return $sContent;
} catch (Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
}
private function getSignatureNonce(int $length): string
{
$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$len = strlen($str) - 1;
$randstr = '';
for ($i = 0; $i < $length; $i++) {
$num = mt_rand(0, $len);
$randstr .= $str[$num];
}
return $randstr;
}
public function textScan($content)
{
$tasks = $this->setQueryUrl($content, '');
print_r($tasks);
}
public function imageScan($content)
{
$tasks = $this->setQueryUrl($content, '');
print_r($tasks);
}
}
$text = new AliyunGreen('id', 'secret', 'cn-beijing', '/green/text/scan',
'');
$body = '{"scenes": ["antispam"],"tasks": [{"dataId": "cfd33235-71a4-468b-8137-a5ffe323a7e8","content": "本校小額貸款,安全、快捷、方便、無抵押,隨機隨貸,當天放款,上門服務。"}]}';
print_r($text->textScan($body));
$image = new AliyunGreen('id', 'secret', 'cn-shenzhen', '/green/image/scan',
'');
$body = '{"scenes":["porn"],"tasks":[{"dataId":"test2NInmO$tAON6qYUrtCRgLo-1mwxdi","url":"https://img.alicdn.com/tfs/TB1urBOQFXXXXbMXFXXXXXXXXXX-1442-257.png"}]}';
print_r($image->imageScan($body));
本作品採用《CC 協議》,轉載必須註明作者和本文連結