123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651 |
- <?php
- namespace crmeb\services;
- use Exception;
- class AlismsService
- {
-
- private $accessKeyId;
-
- private $accessKeySecret;
-
- private $action;
-
- private $endpoints = [
- 'dysmsapi' => [
- 'global' => 'dysmsapi.aliyuncs.com',
- 'cn-hangzhou' => 'dysmsapi.aliyuncs.com',
- 'ap-southeast-1' => 'dysmsapi.ap-southeast-1.aliyuncs.com',
- ],
- 'dybaseapi' => [
- 'global' => 'dybaseapi.aliyuncs.com',
- 'cn-hangzhou' => '1943695596114318.mns.cn-hangzhou.aliyuncs.com',
- ],
- ];
-
- private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
-
- private $signName;
-
- private $verifyPhoneTemplateCode;
-
- private $verifyPhoneTemplateField;
-
- private $baseParams = [
- 'AccessKeyId' => null,
- 'Action' => null,
- 'Format' => 'json',
- 'RegionId' => 'cn-hangzhou',
- 'SignatureMethod' => 'HMAC-SHA1',
- 'SignatureNonce' => null,
- 'SignatureVersion' => '1.0',
- 'Timestamp' => null,
- 'Version' => '2017-05-25',
- ];
-
- private $options = [];
-
- public function __construct($access_key_id = null, $access_key_secret = null)
- {
-
-
-
-
-
-
- if ($access_key_id) {
- $this->accessKeyId = $access_key_id;
- }
- if ($access_key_secret) {
- $this->accessKeySecret = $access_key_secret;
- }
- }
-
- public function setAction($action)
- {
- $this->action = $action;
- return $this;
- }
-
- public function setOption($key, $value)
- {
- $this->options[$key] = $value;
- return $this;
- }
-
- public function hasOption($key)
- {
- return isset($this->options[$key]);
- }
-
- public function setOptions($options, $cover = false)
- {
- if ($cover) {
- foreach ($options as $k => $v) {
- $this->options[$k] = $v;
- }
- } else {
- $this->options = array_merge($this->options, $options);
- }
- return $this;
- }
-
- public function execute()
- {
- $url = $this->endpoints['dysmsapi']['cn-hangzhou'];
- $baseParams = $this->baseParams;
- $baseParams['AccessKeyId'] = $this->accessKeyId;
- $baseParams['Action'] = $this->action;
- $baseParams['SignatureNonce'] = md5(uniqid(mt_rand(), true));
- $baseParams['Timestamp'] = gmdate($this->dateTimeFormat);
-
- $options = array_merge($this->options, $baseParams);
- unset($options['Signature']);
- $options['Signature'] = $this->computeSignature($options);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_FAILONERROR, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $this->parsePostHttpBody($options));
- curl_setopt($ch, CURLOPT_TIMEOUT, 80);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- $http_headers = [
- 'Date:' . gmdate($this->dateTimeFormat),
- 'Accept:application/json',
- 'x-acs-signature-method:HMAC-SHA1',
- 'x-acs-signature-version:1.0',
- 'x-acs-region-id:cn-hangzhou',
- 'x-sdk-client:php/2.0.0',
- 'Content-MD5:' . base64_encode(md5(json_encode($options), true)),
-
- ];
- curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
- $res = curl_exec($ch);
- $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $errno = curl_errno($ch);
- $error = curl_error($ch);
- curl_close($ch);
- if ($errno > 0) {
- throw new Exception($error, $errno);
- }
- $res = json_decode($res, true);
- if (200 != $status || 'OK' != $res['Code']) {
- throw new Exception('ERR[' . $res['Code'] . ']: ' . $res['Message']);
- }
-
- $this->options = [];
- return $res;
- }
-
- public function send($phone_numbers, $template_code, $template_param = null, $sign_name = null, $out_id = null)
- {
- $this->setAction('SendSms');
- if (is_array($phone_numbers)) {
- $phone_numbers = join(',', $phone_numbers);
- }
- $this->setOptions([
- 'PhoneNumbers' => $phone_numbers,
- 'TemplateCode' => $template_code,
- ]);
- if ($sign_name) {
- $this->setOption('SignName', $sign_name);
- }
- if (!$this->hasOption('SignName')) {
- $this->setOption('SignName', $this->signName);
- }
- if (is_string($template_param)) {
- $this->setOption('TemplateParam', $template_param);
- } elseif (is_array($template_param)) {
- $this->setOption('TemplateParam', json_encode($template_param, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
- }
- if (is_string($out_id)) {
- $this->setOption('OutId', $out_id);
- }
- return $this->execute();
- }
-
- public function sendVerifyCode($phone_number, $digit = 6, $verify_code = null, $template_code = null, $field = null)
- {
-
- if (empty($verify_code)) {
- $verify_code = '';
- for ($i = 0; $i < intval($digit); $i++) $verify_code .= mt_rand(0, 9);
- }
- $template_code = empty($template_code) ? $this->verifyPhoneTemplateCode : $template_code;
- $field = empty($field) ? $this->verifyPhoneTemplateField : $field;
- $response = $this->send(
- $phone_number,
- $template_code,
- [$field => $verify_code]
- );
- if ($response['code'] === 'OK' && $response['message'] === 'OK') {
- $response['phone_number'] = $phone_number;
- $response['verify_code'] = $verify_code;
- return $response;
- }
- return false;
- }
-
- public function getDetails($phone_number, $send_date, $current_page = 1, $page_size = 10, $biz_id = null)
- {
- $this->setAction('QuerySendDetails');
- $this->setOptions([
- 'CurrentPage' => $current_page,
- 'PageSize' => $page_size,
- 'PhoneNumber' => $phone_number,
- 'SendDate' => $send_date,
- ]);
- if ($biz_id) {
- $this->setOption('BizId', $biz_id);
- }
- return $this->execute();
- }
-
- private function editSign($sign_name, $sign_source, $remark, $sign_file_list = [])
- {
- $this->setOptions([
- 'SignName' => $sign_name,
- 'SignSource' => $sign_source,
- 'Remark' => $remark,
- ]);
- if (is_array($sign_file_list)) {
- $sign_file_list = array_values($sign_file_list);
- foreach ($sign_file_list as $idx => $sign_file) {
- $this->setOption('SignFileList.' . ($idx + 1) . '.FileSuffix', $sign_file['file_suffix']);
- $this->setOption('SignFileList.' . ($idx + 1) . '.FileContents', $sign_file['file_contents']);
- }
- }
- return $this->execute();
- }
-
- public function addSign($sign_name, $sign_source, $remark, $sign_file_list = [])
- {
- $this->setAction('AddSmsSign');
- return $this->editSign($sign_name, $sign_source, $remark, $sign_file_list);
- }
-
- public function deleteSign($sign_name)
- {
- $this->setAction('DeleteSmsSign');
- $this->setOption('SignName', $sign_name);
- return $this->execute();
- }
-
- public function modifySign($sign_name, $sign_source, $remark, $sign_file_list = [])
- {
- $this->setAction('ModifySmsSign');
- return $this->editSign($sign_name, $sign_source, $remark, $sign_file_list);
- }
-
- public function getSign($sign_name)
- {
- $this->setAction('QuerySmsSign');
- $this->setOption('SignName', $sign_name);
- return $this->execute();
- }
-
- private function editTemplate($template_name, $template_type, $template_content, $remark, $template_code = null)
- {
- $this->setOptions([
- 'TemplateName' => $template_name,
- 'TemplateType' => intval($template_type),
- 'TemplateContent' => $template_content,
- 'Remark' => $remark,
- ]);
- if ($template_code) {
- $this->setOption('TemplateCode', $template_code);
- }
- return $this->execute();
- }
-
- public function addTemplate($template_name, $template_type, $template_content, $remark)
- {
- $this->setAction('AddSmsTemplate');
- return $this->editTemplate($template_name, $template_type, $template_content, $remark);
- }
-
- public function deleteTemplate($template_code)
- {
- $this->setAction('DeleteSmsTemplate');
- $this->setOption('TemplateCode', $template_code);
- return $this->execute();
- }
-
- public function modifyTemplate($template_code, $template_name, $template_type, $template_content, $remark)
- {
- $this->setAction('ModifySmsTemplate');
- return $this->editTemplate($template_name, $template_type, $template_content, $remark, $template_code);
- }
-
- public function getTemplate($template_code)
- {
- $this->setAction('QuerySmsTemplate');
- $this->setOption('TemplateCode', $template_code);
- return $this->execute();
- }
-
- private function percentEncode($str)
- {
- $res = urlencode($str);
- $res = preg_replace('/\+/', '%20', $res);
- $res = preg_replace('/\*/', '%2A', $res);
- $res = preg_replace('/%7E/', '~', $res);
- return $res;
- }
-
- private function computeSignature($params)
- {
- global $access_secret;
- ksort($params);
- $sourceArr = [];
- foreach ($params as $k => $v) {
- $sourceArr[] = $this->percentEncode($k) . '=' . $this->percentEncode($v);
- }
- $source = join('&', $sourceArr);
- $source = 'POST' . '&' . $this->percentEncode('/') . '&' . $this->percentEncode($source);
- return base64_encode(hash_hmac('sha1', $source, $this->accessKeySecret . '&', true));
- }
-
- private function parsePostHttpBody($params)
- {
- $bodyArr = [];
- foreach ($params as $k => $v) {
- $bodyArr[] = $k . '=' . urlencode($v);
- }
- return join('&', $bodyArr);
- }
- }
|