Ali SMS package SDK TP3.2

1. Ali messaging interface requires enterprise certification:

2. SMS text message template needs

 

<?php
/**
* Ali cloud send SMS verification code class
* @param string $accessKeyId key
* @param string $Secret key
* @Param string $ signName SMS template name
* @Param string $ templateCode SMS template code
*/
class Demos {
// save the error messages
public $error;
// Access Key ID
private $accessKeyId = 'LTAIvNlXZGhOPuG8';
// Access Access Key Secret
private $accessKeySecret = 'Xqu7Ul5u1Pt6gyxMuT7sTutn0fjC8E';
public function __construct($cofig = array()) {
$cofig = array (
'accessKeyId' => 'accessKeyId',
'accessKeySecret' => 'accessKeySecret',
'SignName' => 'SMS template name'
);
// configuration parameters
$this->accessKeyId = $cofig ['accessKeyId'];
$this->accessKeySecret = $cofig ['accessKeySecret'];
$this->signName = $cofig ['signName'];
}
private function percentEncode($string) {
$string = urlencode ( $string );
$string = preg_replace ( '/\+/', '%20', $string );
$string = preg_replace ( '/\*/', '%2A', $string );
$string = preg_replace ( '/%7E/', '~', $string );
return $string;
}
/**
* Signed
*
* @param unknown $parameters
* @param unknown $accessKeySecret
* @return string
*/
private function computeSignature($parameters, $accessKeySecret) {
ksort ( $parameters );
$canonicalizedQueryString = '';
foreach ( $parameters 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 ) );
return $signature;
}
/**
* @Param $ mobile phone number
* @param $verify_code
* @Param $ templateCode SMS templates
*/
public function sendTemplateSMS($mobile, $verify_code,$templateCode) {
$params = array (
'SignName' => $this->signName,
'Format' => 'JSON',
'Version' => '2017-05-25',
'AccessKeyId' => $this->accessKeyId,
'SignatureVersion' => '1.0',
'SignatureMethod' => 'HMAC-SHA1',
'SignatureNonce' => uniqid (),
'Timestamp' => gmdate ( 'Y-m-d\TH:i:s\Z' ),
'Action' => 'SendSms',
'TemplateCode' => $templateCode,
'PhoneNumbers' => $mobile,
'TemplateParam' => json_encode($verify_code)
);
// Calculate the signature and the signature added to the results request parameters
$params ['Signature'] = $this->computeSignature ( $params, $this->accessKeySecret );
// send the request (here was modified)
//$url = 'https://sms.aliyuncs.com/?' . http_build_query ( $params );
$url = 'http://dysmsapi.aliyuncs.com/?' . http_build_query ( $params );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 10 );
$result = curl_exec ( $ch );
curl_close ( $ch );
$result = json_decode($result,true);
// if (isset ( $result ['Code'] )) {
// $this->error = $this->getErrorMessage ( $result ['Code'] );
// return false;
// }
return $result;
}
/**
* Get detailed error information
* @param unknown $status
*/
public function getErrorMessage($status) {
// https://api.alidayu.com/doc2/apiDetail?spm=a3142.7629140.1.19.SmdYoA&apiId=25450
$message = array (
'InvalidDayuStatus.Malformed' => 'message accounts open state incorrect',
'InvalidSignName.Malformed' => 'message incorrect signature or signature status is not correct,'
'InvalidTemplateCode.MalFormed' => 'Code message template or template incorrect state incorrect',
'InvalidRecNum.Malformed' => 'target phone number is incorrect, a single transmission can not exceed the number 100',
'InvalidParamString.MalFormed' => 'message template variables are not json format'
'InvalidParamStringTemplate.Malformed' => 'message template variable contents do not match the template',
'InvalidSendSms' => 'traffic flow control trigger',
'InvalidDayu.Malformed' => 'variables can not url, variables can be cured in the template'
);
if (isset ( $message [$status] )) {
return $message [$status];
}
return $status;
}
}

Guess you like

Origin www.cnblogs.com/PLasir/p/11447408.html