SMS.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. //漫步科技的接口
  3. function SendSMSMessage($phone, $content)
  4. {
  5. $content = str_replace('【','(',$content);
  6. $content = str_replace('】',')',$content);
  7. Yii::log("phone:" . $phone . " content:" . $content);
  8. $content = $content . "【乐外卖】";
  9. $content = iconv("utf-8", "gb2312//TRANSLIT//IGNORE", $content);
  10. $flag = 0;
  11. //要post的数据
  12. $argv = array(
  13. 'sn'=>'SDK-JMR-010-00033', ////替换成您自己的序列号
  14. 'pwd'=>strtoupper(md5('SDK-JMR-010-00033'.'893025')), //此处密码需要加密 加密方式为 md5(sn+password) 32位大写
  15. 'mobile'=>$phone,//手机号 多个用英文的逗号隔开 post理论没有长度限制.推荐群发一次小于等于10000个手机号
  16. 'content'=>$content,//短信内容
  17. 'ext'=>'',
  18. 'stime'=>'',//定时时间 格式为2011-6-29 11:09:21
  19. 'rrid'=>''
  20. );
  21. //构造要post的字符串
  22. $params = "";
  23. foreach ($argv as $key=>$value) {
  24. if ($flag!=0) {
  25. $params .= "&";
  26. $flag = 1;
  27. }
  28. $params.= $key."="; $params.= urlencode($value);
  29. $flag = 1;
  30. }
  31. $length = strlen($params);
  32. //创建socket连接
  33. $fp = fsockopen("sdk2.entinfo.cn",8060,$errno,$errstr,10) or exit($errstr."--->".$errno);
  34. //构造post请求的头
  35. $header = "POST /webservice.asmx/mt HTTP/1.1\r\n";
  36. $header .= "Host:sdk2.entinfo.cn\r\n";
  37. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  38. $header .= "Content-Length: ".$length."\r\n";
  39. $header .= "Connection: Close\r\n\r\n";
  40. //添加post的字符串
  41. $header .= $params."\r\n";
  42. //发送post的数据
  43. fputs($fp,$header);
  44. $inheader = 1;
  45. while (!feof($fp)) {
  46. $line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据
  47. if ($inheader && ($line == "\n" || $line == "\r\n")) {
  48. $inheader = 0;
  49. }
  50. if ($inheader == 0) {
  51. // echo $line;
  52. }
  53. }
  54. //<string xmlns="http://tempuri.org/">-5</string>
  55. $line=str_replace("<string xmlns=\"http://tempuri.org/\">","",$line);
  56. $line=str_replace("</string>","",$line);
  57. $result=explode("-",$line);
  58. // echo $line."-------------";
  59. if(count($result)>1)
  60. {
  61. Yii::log('发送失败返回值为:'.$line.'。请查看webservice返回值对照表');
  62. return false;
  63. }
  64. else
  65. {
  66. Yii::log('发送成功 返回值为:'.$line);
  67. return true;
  68. }
  69. }
  70. //互亿无线的接口
  71. function Post($curlPost,$url){
  72. $curl = curl_init();
  73. curl_setopt($curl, CURLOPT_URL, $url);
  74. curl_setopt($curl, CURLOPT_HEADER, false);
  75. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  76. curl_setopt($curl, CURLOPT_NOBODY, true);
  77. curl_setopt($curl, CURLOPT_POST, true);
  78. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
  79. $return_str = curl_exec($curl);
  80. curl_close($curl);
  81. return $return_str;
  82. }
  83. function xml_to_array($xml){
  84. $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
  85. if(preg_match_all($reg, $xml, $matches)){
  86. $count = count($matches[0]);
  87. for($i = 0; $i < $count; $i++){
  88. $subxml= $matches[2][$i];
  89. $key = $matches[1][$i];
  90. if(preg_match( $reg, $subxml )){
  91. $arr[$key] = xml_to_array( $subxml );
  92. }else{
  93. $arr[$key] = $subxml;
  94. }
  95. }
  96. }
  97. return $arr;
  98. }
  99. function SendSMSMessage2($phone, $content)
  100. {
  101. $target = "http://106.ihuyi.com/webservice/sms.php?method=Submit";
  102. $post_data = "account=cf_xxkj&password=hhyz122131&mobile=".$phone."&content=".rawurlencode($content);
  103. //密码可以使用明文密码或使用32位MD5加密
  104. Yii::log("post_data:" . $post_data);
  105. $gets = xml_to_array(Post($post_data, $target));
  106. if ($gets['SubmitResult']['code'] == 2)
  107. {
  108. return true;
  109. }
  110. else
  111. {
  112. Yii::log("SMSErrorCode:" . $gets['SubmitResult']['code'] . " SMSResult:" . $gets['SubmitResult']['msg']);
  113. return false;
  114. }
  115. }
  116. /*------------云信使的接口--------------------
  117. 功能: HTTP接口 发送短信
  118. 修改日期: 2011-03-04
  119. 说明: http://api.sms.cn/mt/?uid=用户账号&pwd=MD5位32密码&mobile=号码&mobileids=号码编号&content=内容
  120. 状态:
  121. 100 发送成功
  122. 101 验证失败
  123. 102 短信不足
  124. 103 操作失败
  125. 104 非法字符
  126. 105 内容过多
  127. 106 号码过多
  128. 107 频率过快
  129. 108 号码内容空
  130. 109 账号冻结
  131. 110 禁止频繁单条发送
  132. 112 号码不正确
  133. 120 系统升级
  134. --------------------------------*/
  135. //定时发送
  136. /*
  137. $time = '2010-05-27 12:11';
  138. $res = sendSMS($uid,$pwd,$mobile,$content,$time);
  139. echo $res;
  140. */
  141. function postSMS($url,$data='')
  142. {
  143. $row = parse_url($url);
  144. $host = $row['host'];
  145. $port = isset($row['port']) ? $row['port']:80;
  146. $file = $row['path'];
  147. $post = '';
  148. while (list($k,$v) = each($data))
  149. {
  150. $post .= rawurlencode($k)."=".rawurlencode($v)."&"; //转URL标准码
  151. }
  152. $post = substr( $post , 0 , -1 );
  153. $len = strlen($post);
  154. $fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
  155. if (!$fp) {
  156. return "$errstr ($errno)\n";
  157. } else {
  158. $receive = '';
  159. $out = "POST $file HTTP/1.1\r\n";
  160. $out .= "Host: $host\r\n";
  161. $out .= "Content-type: application/x-www-form-urlencoded\r\n";
  162. $out .= "Connection: Close\r\n";
  163. $out .= "Content-Length: $len\r\n\r\n";
  164. $out .= $post;
  165. fwrite($fp, $out);
  166. while (!feof($fp)) {
  167. $receive .= fgets($fp, 128);
  168. }
  169. fclose($fp);
  170. $receive = explode("\r\n\r\n",$receive);
  171. unset($receive[0]);
  172. return implode("",$receive);
  173. }
  174. }
  175. function SendSMSMessage3($mobile,$content,$mobileids='',$time='',$mid='')
  176. {
  177. $http = 'http://api.sms.cn/mt/'; //短信接口
  178. $uid = 'liantongxue'; //用户账号
  179. $pwd = 'hhyz122131'; //密码
  180. $content = $content . "【乐外卖】";
  181. $content = iconv("utf-8", "gb2312//TRANSLIT", $content);
  182. $data = array
  183. (
  184. 'uid'=>$uid, //用户账号
  185. 'pwd'=>md5($pwd.$uid), //MD5位32密码,密码和用户名拼接字符
  186. 'mobile'=>$mobile, //号码
  187. 'content'=>$content, //内容
  188. 'mobileids'=>$mobileids,
  189. 'time'=>$time, //定时发送
  190. );
  191. $re= postSMS($http,$data); //POST方式提交
  192. $re = substr($re, 9, 3);
  193. Yii::log("re:" . $re);
  194. if($re == '100')
  195. {
  196. return true;
  197. }
  198. else
  199. {
  200. Yii::log("发送失败! 状态:" . $re);
  201. return false;
  202. }
  203. }
  204. function GetRandomNum()
  205. {
  206. $str = '0123456789';
  207. $rndcode = rand(0,9);
  208. $rndNum = $str[$rndcode];
  209. return $rndNum;
  210. }
  211. function SendCaptcha($phone, $shopname)
  212. {
  213. //先生成一个随机的6位数验证码
  214. $Char1 = GetRandomNum();
  215. $Char2 = GetRandomNum();
  216. $Char3 = GetRandomNum();
  217. $Char4 = GetRandomNum();
  218. $Char5 = GetRandomNum();
  219. $Char6 = GetRandomNum();
  220. $captcha = $Char1 . $Char2 . $Char3 . $Char4 . $Char5 . $Char6;
  221. $content = "验证码:" . $captcha . ",请在店铺“" . $shopname . "”微信下单页面填写该验证码。";
  222. Yii::log("content:" . $content);
  223. $ret = SendSMSMessage($phone, $content);
  224. if ($ret == true)
  225. {
  226. return $captcha;
  227. }
  228. return false;
  229. }
  230. ?>