php调用阿里云的短信服务

2021-02-22  本文已影响0人  一一秋风
<?php
// 引入自动加载
require __DIR__ . '/vendor/autoload.php';
 
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
 
$accessKeyId='*********';
 
$accessSecret='*********************';
 
// APP传过来的accesstoken
$AccessToken='****************************************************';
 
AlibabaCloud::accessKeyClient($accessKeyId, $accessSecret)
                        ->regionId('cn-hangzhou')
                        ->asDefaultClient();
 
try {
    $result = AlibabaCloud::rpc()
                          ->product('Dypnsapi')
                          ->scheme('https') // 设置协议头 https | http
                          ->version('2017-05-25')
                          ->action('GetMobile')
                          ->method('POST')
                          ->host('dypnsapi.aliyuncs.com')
                          ->options([
                                        'query' => [
                                          'RegionId' => "cn-hangzhou",
                                          'AccessToken' => $accessToken,
                                          'PhoneNumbers' => $phone,
                                          'SignName' => $config['SignName'],
                                          'TemplateCode' => $config['TemplateCode'],
                                          'TemplateParam' => json_encode($param)
                                        ],
                                    ])
                          ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}
上一篇下一篇

猜你喜欢

热点阅读