hyperf 使用阿里云短信

<?php
declare(strict_types=1);

namespace Service\ALiSms;

use Hyperf\Di\Annotation\Inject;
use Hyperf\Guzzle\ClientFactory;

class ALiSmsService
{
    /**
     * @var \Hyperf\Guzzle\ClientFactory
     */
    private $clientFactory;
    private $url = 'http://dysmsapi.aliyuncs.com/?';
    public function __construct(ClientFactory $clientFactory)
    {
        $this->clientFactory = $clientFactory;
    }
    private function percentEncode($string) {
        $string = urlencode ( $string );
        $string = preg_replace ( '/\+/', '%20', $string );
        $string = preg_replace ( '/\*/', '%2A', $string );
        $string = preg_replace ( '/%7E/', '~', $string );
        return $string;
    }
    protected function getPublicParam()
    {
        return  $params = array (
            'Version' => '2017-05-25',
            'Timestamp' => gmdate ( 'Y-m-d\TH:i:s\Z' ),
            'SignatureVersion' => '1.0',
            'SignatureNonce' => uniqid (),
            'SignatureMethod' => 'HMAC-SHA1',
            'Format' => 'JSON'
        );
    }
    protected function getSign(string $accessKeySecret,$array=array())
    {
        $params = $this->getPublicParam();
        $newArray = array_merge($params,$array);
        unset($newArray['Signature']);
        ksort ( $newArray );
        $canonicalizedQueryString = '';
        foreach ( $newArray as $key => $value ) {
            $canonicalizedQueryString .= '&' . $this->percentEncode ( $key ) . '=' . $this->percentEncode ( $value );
        }
        $stringToSign = 'GET&%2F&' . $this->percentencode ( substr ( $canonicalizedQueryString, 1 ) );
        $signature = base64_encode ( hash_hmac ( 'sha1', $stringToSign, $accessKeySecret . '&', true ) );
        $newArray ['Signature'] = $signature;
        $client = $this->clientFactory->create($options=[]);
        $url = $this->url . http_build_query ( $newArray );
        $result = $client->request('get',$url);
        return json_decode($result->getBody()->getContents(),true);
    }
    public function sendSms(string $accessKeySecret,$array=array())
    {
        $array['Action']='SendSms';
        return $this->getSign($accessKeySecret,$array);
    }
    public function phoneNumberJson(string $accessKeySecret,$array=array())
    {
        $array['Action']='SendBatchSms';
        return $this->getSign($accessKeySecret,$array);
    }
    public function getSendDetails(string $accessKeySecret,$array=array())
    {
        $array['Action']='QuerySendDetails';
        return $this->getSign($accessKeySecret,$array);
    }
}

发送单条

 $params = array (
        'SignName' => '签名',
        'AccessKeyId' => 'xxx',
        'TemplateCode' => '模板id',
        'PhoneNumbers' => '1xxxxxxx',
        'TemplateParam' => '{"code":"1234"}',
        'RegionId' => 'cn-beijing',
    );

phoneNumberJson(批量发送)

  $params = array (
        'PhoneNumberJson'=>json_encode(array('手机号')),
        'SignNameJson'=>json_encode(array('签名')),
        'TemplateCode'=>'模板id',
        'AccessKeyId' => 'xxxx',
        'TemplateParamJson'=>json_encode(array(array('code'=>'0000'))),
    );

getSendDetails(查询发送记录)

$params = array (
                'CurrentPage'=>'1',
                'PageSize'=>'30',
                'PhoneNumber'=>'手机号',
                'SendDate'=>'20191210',
                'AccessKeyId' => 'xxxx',
            );  

调用

$accessKeySecret='xxxx';
$this->ALiSmsService->getSendDetails($accessKeySecret,$params);

返回结果具体参数看阿里云.返回格式是数组格式.

array(4) {
  ["Message"]=>
  string(2) "OK"
  ["RequestId"]=>
  string(36) "9FFC1339-249D-4335-9DBE-87843DA315CB"
  ["BizId"]=>
  string(20) "785908076031923306^0"
  ["Code"]=>
  string(2) "OK"
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
1
粉丝
1
喜欢
0
收藏
1
排名:2152
访问:799
私信
所有博文
博客标签
社区赞助商