sms.class.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tangqiang
  5. * Date: 17/5/11
  6. * Time: 下午10:19
  7. */
  8. class sms{
  9. const MSG_KEY = "7f9a5d7391bf8a65e549b811db846d02";
  10. const SEND_URL= "http://v.juhe.cn/sms/send"; //短信接口的URL
  11. // const CODE_TPL_ID = "145531";
  12. const TPLS = array(
  13. '216187' => array('name' => '预约成功通知', 'content' => '【广庭上医】您已成功预约了本次健康咨询服务,预约时间为:#ordertime#。如您有事需要取消预约,请您于#reversetime#前登陆广庭上医健康管理平台取消预约。', 'params' => array('ordertime', 'reversetime')),
  14. '216188' => array('name' => '取消预约通知', 'content' => '【广庭上医】您已成功取消#canceltime#的健康咨询服务预约。', 'params' => array('canceltime')),
  15. // '145531' => array('name' => '验证码', 'content' => '【广庭上医】您的验证码是#code#。', 'params' => array('code')),
  16. );
  17. /**
  18. * 返回短信模板列表
  19. */
  20. public static function getTpls() {
  21. return self::TPLS;
  22. }
  23. public static function send_by_tpl($mobile, $tpl_id, $tpl_value) {
  24. $smsConf = array(
  25. 'key' => self::MSG_KEY, //您申请的APPKEY
  26. 'mobile' => $mobile, //接受短信的用户手机号码
  27. 'tpl_id' => $tpl_id, //您申请的短信模板ID,根据实际情况修改
  28. 'tpl_value' => $tpl_value, //您设置的模板变量,根据实际情况修改
  29. );
  30. $res = self::juhecurl(self::SEND_URL,$smsConf,1); //请求发送短信
  31. //print_r($res);
  32. //exit();
  33. if($res){
  34. $result = json_decode($res,true);
  35. $error_code = $result['error_code'];
  36. if($error_code == 0){
  37. return array('code' => 0, 'msg' => '发送成功');
  38. }else{
  39. return array('code' => 1, 'msg' => $result['reason']);
  40. }
  41. }else{
  42. //返回内容异常,以下可根据业务逻辑自行修改
  43. return array('code' => 1, 'msg' => '请求返回值异常');
  44. }
  45. }
  46. /** 取消预约 您已成功取消#canceltime#的健康咨询服务预约。
  47. * @param $mobile 手机号
  48. * @param $canceltime 取消时间(字符串类型)
  49. */
  50. public static function cancelReservation($mobile,$canceltime){
  51. $ret=self::send_by_tpl($mobile, '216188', '#canceltime#='.$canceltime);
  52. $member = Member::getInfoByPhone($mobile);
  53. $attrs = array(
  54. 'memberid' => $member['id'],
  55. 'time'=>time(),
  56. 'content'=>"您已成功取消$canceltime 的健康咨询服务预约。",
  57. 'mobile'=>$mobile,
  58. 'tplid'=>'216188',
  59. 'tplvalue'=>'#canceltime#='.$canceltime,
  60. );
  61. if($ret['code']==0){
  62. $attrs['status']=1;
  63. $attrs['failcount']=0;
  64. $attrs['failreason']="";
  65. }else{
  66. $attrs['status']=2;
  67. $attrs['failcount']=1;
  68. $attrs['failreason']=$ret['msg'];
  69. }
  70. Pushsms::add($attrs);
  71. return $ret;
  72. }
  73. /** 预约 您已成功预约了本次健康咨询服务,预约时间为:#ordertime#。如您有事需要取消预约,请您于#reversetime#前登陆广庭上医健康管理平台取消预约。
  74. * @param $mobile 手机号
  75. * @param $ordertime 预约时间(字符串类型)
  76. * @param $reversetime 截止取消时间(字符串类型)
  77. */
  78. public static function reservation($mobile,$ordertime,$reversetime){
  79. $ret= self::send_by_tpl($mobile, '216187', '#ordertime#='.$ordertime.'&'.'#reversetime#='.$reversetime);
  80. $member = Member::getInfoByPhone($mobile);
  81. $attrs = array(
  82. 'memberid' => $member['id'],
  83. 'time'=>time(),
  84. 'content'=>"您已成功预约了本次健康咨询服务,预约时间为:$ordertime 。如您有事需要取消预约,请您于 $reversetime 前登陆广庭上医健康管理平台取消预约。",
  85. 'mobile'=>$mobile,
  86. 'tplid'=>'216187',
  87. 'tplvalue'=>'#ordertime#='.$ordertime.'&'.'#reversetime#='.$reversetime,
  88. );
  89. if($ret['code']==0){
  90. $attrs['status']=1;
  91. $attrs['failcount']=0;
  92. $attrs['failreason']="";
  93. }else{
  94. $attrs['status']=2;
  95. $attrs['failcount']=1;
  96. $attrs['failreason']=$ret['msg'];
  97. }
  98. Pushsms::add($attrs);
  99. return $ret;
  100. }
  101. // static public function send_code($mobile,$code)
  102. // {
  103. // return self::send_by_tpl($mobile, self::CODE_TPL_ID, '#code#='.$code);
  104. // }
  105. static public function buildContent($tplid, $params)
  106. {
  107. $tpl = sms::getTpls()[$tplid];
  108. $parsedContent = $tpl['content'];
  109. foreach ($params as $key => $value) {
  110. $parsedContent = str_replace('#'.$key.'#', $value, $parsedContent);
  111. }
  112. return $parsedContent;
  113. }
  114. static public function buildParamString($params)
  115. {
  116. $keyValueArray = array();
  117. foreach ($params as $key => $value) {
  118. $keyValueArray[] = '#'.$key.'#='.$value;
  119. }
  120. return join('&', $keyValueArray);
  121. }
  122. /**
  123. * 请求接口返回内容
  124. * @param string $url [请求的URL地址]
  125. * @param string $params [请求的参数]
  126. * @param int $ipost [是否采用POST形式]
  127. * @return string
  128. */
  129. static function juhecurl($url,$params=false,$ispost=0){
  130. $httpInfo = array();
  131. $ch = curl_init();
  132. curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
  133. curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
  134. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
  135. curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
  136. curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
  137. if( $ispost )
  138. {
  139. curl_setopt( $ch , CURLOPT_POST , true );
  140. curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
  141. curl_setopt( $ch , CURLOPT_URL , $url );
  142. }
  143. else
  144. {
  145. if($params){
  146. curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
  147. }else{
  148. curl_setopt( $ch , CURLOPT_URL , $url);
  149. }
  150. }
  151. $response = curl_exec( $ch );
  152. if ($response === FALSE) {
  153. //echo "cURL Error: " . curl_error($ch);
  154. return false;
  155. }
  156. $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
  157. $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
  158. curl_close( $ch );
  159. return $response;
  160. }
  161. }