Dayu.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. //+----------------------------------------------------------------------
  3. //| 版权所有 YI ,并保留所有权利
  4. //| 这不是一个自由软件!禁止任何形式的拷贝、修改、发布
  5. //+----------------------------------------------------------------------
  6. //| 开发者: YI
  7. //| 时间 : 12:14
  8. //+----------------------------------------------------------------------
  9. /** 阿里云短信平台
  10. * Class Dayu
  11. */
  12. class Dayu
  13. {
  14. protected $accessKeyId;
  15. protected $accessKeySecret;
  16. protected $SignName;
  17. protected $TemplateCode;
  18. /** 初始化参数
  19. * Dayu constructor.
  20. * @param $accessKeyId 账号
  21. * @param $accessKeySecret 密码
  22. * @param $SignName 签名
  23. * @param $TemplateCode 模板id
  24. */
  25. function __construct($accessKeyId,$accessKeySecret,$SignName,$TemplateCode)
  26. {
  27. $this->accessKeyId = $accessKeyId;
  28. $this->accessKeySecret = $accessKeySecret;
  29. $this->SignName = $SignName;
  30. $this->TemplateCode = $TemplateCode;
  31. }
  32. /**
  33. * @param $mobile 手机号码
  34. * @param $code $code 验证码
  35. * @return bool|stdClass
  36. */
  37. public function sendSms($mobile,$code)
  38. {
  39. $security = false;
  40. $params = [
  41. 'PhoneNumbers' => $mobile,
  42. 'SignName' => $this->SignName,
  43. 'TemplateCode' => $this->TemplateCode,
  44. 'TemplateParam' => json_encode(['code'=>$code]),
  45. ];
  46. // 此处可能会抛出异常,注意catch
  47. $content = $this->request(
  48. $this->accessKeyId,
  49. $this->accessKeySecret,
  50. "dysmsapi.aliyuncs.com",
  51. array_merge($params, array(
  52. "RegionId" => "cn-hangzhou",
  53. "Action" => "SendSms",
  54. "Version" => "2017-05-25",
  55. )),
  56. $security
  57. );
  58. return $content;
  59. }
  60. /**
  61. * 生成签名并发起请求
  62. *
  63. * @param $accessKeyId string AccessKeyId (https://ak-console.aliyun.com/)
  64. * @param $accessKeySecret string AccessKeySecret
  65. * @param $domain string API接口所在域名
  66. * @param $params array API具体参数
  67. * @param $security boolean 使用https
  68. * @param $method boolean 使用GET或POST方法请求,VPC仅支持POST
  69. * @return bool|\stdClass 返回API接口调用结果,当发生错误时返回false
  70. */
  71. public function request($accessKeyId, $accessKeySecret, $domain, $params, $security=false, $method='POST') {
  72. $apiParams = array_merge(array (
  73. "SignatureMethod" => "HMAC-SHA1",
  74. "SignatureNonce" => uniqid(mt_rand(0,0xffff), true),
  75. "SignatureVersion" => "1.0",
  76. "AccessKeyId" => $accessKeyId,
  77. "Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
  78. "Format" => "JSON",
  79. ), $params);
  80. ksort($apiParams);
  81. $sortedQueryStringTmp = "";
  82. foreach ($apiParams as $key => $value) {
  83. $sortedQueryStringTmp .= "&" . $this->encode($key) . "=" . $this->encode($value);
  84. }
  85. $stringToSign = "${method}&%2F&" . $this->encode(substr($sortedQueryStringTmp, 1));
  86. $sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&",true));
  87. $signature = $this->encode($sign);
  88. $url = ($security ? 'https' : 'http')."://{$domain}/";
  89. try {
  90. $content = $this->fetchContent($url, $method, "Signature={$signature}{$sortedQueryStringTmp}");
  91. return json_decode($content,true);
  92. } catch( \Exception $e) {
  93. return ['code'=>'ERROR'];
  94. }
  95. }
  96. private function encode($str)
  97. {
  98. $res = urlencode($str);
  99. $res = preg_replace("/\+/", "%20", $res);
  100. $res = preg_replace("/\*/", "%2A", $res);
  101. $res = preg_replace("/%7E/", "~", $res);
  102. return $res;
  103. }
  104. private function fetchContent($url, $method, $body) {
  105. $ch = curl_init();
  106. if($method == 'POST') {
  107. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  108. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  109. } else {
  110. $url .= '?'.$body;
  111. }
  112. curl_setopt($ch, CURLOPT_URL, $url);
  113. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  114. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  115. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  116. "x-sdk-client" => "php/2.0.0"
  117. ));
  118. if(substr($url, 0,5) == 'https') {
  119. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  120. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  121. }
  122. $rtn = curl_exec($ch);
  123. if($rtn === false) {
  124. // 大多由设置等原因引起,一般无法保障后续逻辑正常执行,
  125. // 所以这里触发的是E_USER_ERROR,会终止脚本执行,无法被try...catch捕获,需要用户排查环境、网络等故障
  126. trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
  127. }
  128. curl_close($ch);
  129. return $rtn;
  130. }
  131. }