阿里云内容安全文字图片安全审核评级php示例代码

代码语言:php

所属分类:其他

代码描述:阿里云内容安全文字图片安全审核评级php示例代码,可对文字和图片进行违规识别。

代码标签: 阿里云 内容安全 文字 图片 安全 审核 评级 php 示例 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php
//先开通,然后使用accessid和key来使用
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;
    }

    /**
     * @param string $body
     * @param string $clientInfo
     * @return mixed
     */
    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);
    }

    /**
     * @param string $body
     * @return array
     */
    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()), //'Tue, 17 Jan 2017 10:16:36 GMT',注意时区
            '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;
    }

    /**
     * @param string $url
     * @param bool $isPost
     * @param $arr
     * @param array $header
     * @return bool|string
     */
    public function curl(string $url, bool $isPost, $arr, array $header)
    {
        try {
            $ch = curl_init();
            if ($ch === fal.........完整代码请登录后点击上方下载按钮下载查看

网友评论0