PHP 接入阿里云短信/语音接口

首先要安装阿里云的依赖包 以下代码仅供参考

下面->action中的方法就是阿里云文档中对应的方法



namespace App\Http\Controllers\Plugin;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use Illuminate\Support\Facades\Log;

class Send
{
    protected $CalledNumber;
    protected $templateCode;
    protected $params;

    public function __construct($CalledNumber,string $templateCode,string $params)
    {
        $this->CalledNumber = $CalledNumber;
        $this->templateCode = $templateCode;
        $this->params = $params;
    }

    /**
     *  发送短信
     * @return array|void
     * @throws ClientException
     */
    public function sendSms(){
         AlibabaCloud::accessKeyClient(config('aliyun.access_key_id'), config('aliyun.access_key_secret'))
            ->regionId(config('aliyun.region_id'))//设置地域ID
            ->asDefaultClient();

        try {
            //SendSms短信通知
            $product = 'Dysmsapi';
            $paramsKey = 'TemplateParam';
            $query = [
                'RegionId' => config('aliyun.region_id'),
                'PhoneNumbers' => $this->CalledNumber,
                'SignName' => config('aliyun.call_sb_sign_name'),
                'TemplateCode' => $this->templateCode,
            ];

            if($this->params){
                $this->params = [$paramsKey=>$this->params];
                $query = array_merge($query,$this->params);
            }
            $result = AlibabaCloud::rpc()
                ->product($product)
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host($product.'.aliyuncs.com')
                ->options([
                    'query' => $query,
                ])
                ->request();
            return $result->toArray();
        } catch (ClientException $e) {
            Log::error($e->getMessage(),['uri'=>\request()->path()]);
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            Log::error($e->getMessage(),['uri'=>\request()->path()]);
            echo $e->getErrorMessage() . PHP_EOL;
        }
    }

    /**
     *  发送语音
     *
     * @param $argv
     * @return array|\Illuminate\Http\JsonResponse
     * @throws ClientException
     */
    public static function sendVoice($argv)
    {
        AlibabaCloud::accessKeyClient(config('aliyun.access_key_id'), config('aliyun.access_key_secret'))
            ->asDefaultClient();

        try {
            $product = 'dyvmsapi';
            $query = [
                'CalledNumber' => $argv['CalledNumber'],//要发送的手机号
                'TtsCode' => $argv['TtsCode'],//语音模板
            ];
            $result = AlibabaCloud::rpc()
                ->product($product)
                ->version('2017-05-25')
                ->action('SingleCallByTts')
                ->method('POST')
                ->host($product.'.aliyuncs.com')
                ->options([
                    'query' => $query,
                ])
                ->request();
            return $result->toArray();
        } catch (ClientException|ServerException $e) {
            Log::error($e->getMessage(),['uri'=>\request()->path()]);
            echo $e->getErrorMessage() . PHP_EOL;
        }
    }

}

你可能感兴趣的:(PHP,php,阿里云)