中國電信翼聊短信可以向三網用戶發送短信,每天對每一個號碼可以最多發送5條短信,當發送5條短信的時候會出現驗證碼,但是只要輸入一次驗證碼之后就可以繼續使用,不再需要輸入驗證碼,所以還是有一定實用價值的。下面是PHP 翼聊短信發送類源代碼。暫不支持放置在多出口的主機上,因為翼聊短信是單點登錄的,限制只有一個用戶和一個IP在線,所以SAE環境無法非常正常的使用,會有間歇性故障,原因是出口IP問題。
下面就是代碼了
<?php/**翼聊短信發送類,由于翼聊短信限制,非電信用戶每天對每個號碼不能發送5條以上的短信,發送多條時會出現驗證碼,所以會發送失敗;*其實只要輸入驗證碼就好了,反正翼聊短信也就驗證一次驗證碼~*Author:CplusHua*URI:http://weibo.com/sdnugonghua*/Class smsPush{ html' target='_blank'>public $PhoneNum; public $vcode; public $cookie; function __construct($PhoneNum=null){ if(null==$PhoneNum) echo '您沒有設置發送者手機號碼,這樣是發不出短信滴~'; $this->PhoneNum=$PhoneNum; } function __get($var){ return $this->$var; } function getVcode($PhoneNum=null){ if($PhoneNum==null&& $this->PhoneNum!=null) $PhoneNum=$this->PhoneNum; else if($PhoneNum==null) return false; $url='http://115.239.133.251:6090/imweb/phoneCheckCode.s?0.9838632841128856&sendPhone='.$PhoneNum.'&methodType=getPhoneCode'; $option=array( CURLOPT_URL=>$url, CURLOPT_POST=>false, CURLOPT_RETURNTRANSFER=>true, //CURLOPT_HEADER=>true, ); $result=$this->exec($option); $this->saveCookie($this->cookie); if(100==$result) return true; else return false; } function SubmitVcode($PhoneNum=null,$vcode=null){ if(null==$vcode) $vcode=$this->vcode; if(null==$PhoneNum) $PhoneNum=$this->PhoneNum; $this->cookie='JSESSIONID=C72FD92F73AB532C0676565D2D8B7971;loginType=1; firsstYZ=yes'; $option=array( CURLOPT_URL=>'http://115.239.133.251:6090/imweb/codeLogin.s?clientId=46&account='.$PhoneNum.'&checkCode='.$vcode.'&rid=0.19796998496167362', CURLOPT_COOKIE=>$this->cookie, CURLOPT_HEADER=>true, CURLOPT_RETURNTRANSFER=>true, ); $result=$this->exec($option); preg_match_all('//nSet-Cookie:/s(.*)/s/n[/w|/W]*({"code":"100","loginSessionInfo":{.*})/i', $result, $matches); //print_r($matches);//echo $matches[0][1]; $res=json_decode($matches[2][0]); // print_r($res); if(100!=$res->code) return false; $this->cookie=$matches[1][0]; $this->saveCookie($this->cookie); return true; } function exec($option=array()){ if(empty($option)) return false; $c=curl_init(); curl_setopt_array($c, $option); $res=curl_exec($c); curl_close($c); return $res; } function sendSMS($receivePhone=null,$msg=null,$checkCode=null){ if(null==$receivePhone||null==$msg) return false; $data='&checkCode='.$checkCode.'&receivePhone='.$receivePhone.'&smsContent='.$msg.'&random=0.7006821087561548'; $this->readCookie(); $this->cookie.=';loginType=1; firsstYZ=yes'; //echo $this->cookie; $option = array( CURLOPT_URL =>'http://115.239.133.251:6090/imweb/smsPush.s?clientId=46' , CURLOPT_POST=>true, CURLOPT_POSTFIELDS=>$data, CURLOPT_COOKIE=>$this->cookie, CURLOPT_RETURNTRANSFER=>true, ); $result=$this->exec($option);echo $result; if(101==$result){ echo "這個IP還未登錄,如果您是用在了分布式服務器,那么肯定是因為出口的IP不一樣了~"; return $result; } if(104==$result){ echo "發送次數超限!"; return $result; } if(201==$result) { echo '請輸入驗證碼!就這樣悲劇了~';//其實就輸入一次,抓取回來輸入進去不就完事了嘛~ 人家翼聊短信是記錄IP的,不要用多IP的服務器,否則悲劇了~ return $result; } if(100==$result) return true; else return $result; } function sae_saveCookie($string){ $mmc=memcache_init(); if($mmc==false){ echo "mc init failed/n"; return 0; } else { return memcache_set($mmc,$this->PhoneNum,$string); } } function sae_readCookie(){ $mmc=memcache_init(); if($mmc==false){ echo "mc init failed/n"; return 0; } else { return memcache_get($mmc,$this->PhoneNum); } } //雖然兼容了SAE環境的寫cookie問題,但是SAE是多線出口,所以沒有辦法保證每次的出口IP都一樣。使用該cookie,出口IP不同的時候是無法使用的 function saveCookie($string){ if(!empty($_SERVER['HTTP_APPNAME'])&&!empty($_SERVER['HTTP_APPVERSION'])) return $this->sae_saveCookie($string); $f=fopen($this->PhoneNum.'.txt', 'w'); return fwrite($f, $string); } function readCookie(){ if(isset($_SERVER['HTTP_APPNAME'])&&isset($_SERVER['HTTP_APPVERSION'])){ $this->cookie= $this->sae_readCookie(); return 1;} if(filesize($this->PhoneNum.'.txt')){ $f=fopen($this->PhoneNum.'.txt', 'r'); $cookie=fread($f, filesize($this->PhoneNum.'.txt')); if(!empty($cookie)) return $this->cookie=$cookie; } } function setImgcode(){ } function getImg($imgurl="http://115.239.133.251:6090/imweb/imageServlet.s",$reffer="http://liao.189.cn/"){ $this->readCookie(); $this->cookie.=';loginType=1; firsstYZ=yes'; //echo $this->cookie; $option = array( CURLOPT_URL =>$imgurl , CURLOPT_RETURNTRANSFER=>true, CURLOPT_HEADER=>0, CURLOPT_USERAGENT=>'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; QQDownload 1.7; TencentTraveler 4.0', CURLOPT_REFERER=>'http://liao.189.cn/', CURLOPT_COOKIE=>$this->cookie, CURLOPT_COOKIESESSION=>true, ); $result=$this->exec($option); header('Content-Type: image/jpeg'); echo $result; return $result; }}
使用方法:
獲取手機驗證碼: www.it165.net
<?phpinclude 'liao.class.php';if(isset($_GET['tel'])){ $obj=new smsPush($_GET['tel']); $res=$obj->getVcode($_GET['tel']); if($res) echo '獲取驗證碼成功!';}else{ echo "要這樣獲取驗證碼哦~";
提交手機驗證碼并登陸:
<?phpinclude 'liao.class.php';if(isset($_GET['tel'])&&isset($_GET['vcode'])){ $obj=new smsPush($_GET['tel']); $res=$obj->SubmitVcode($_GET['tel'],$_GET['vcode']); if($res) echo "成功登錄";}else{ echo "要這樣登錄哦~"; echo "http://".$_SERVER['HTTP_HOST']."/login.php?tel=12345567&vcode=驗證碼";}
發送短信:
<?phpinclude 'liao.class.php';if(isset($_GET['tel'])&&isset($_GET['text'])&&isset($_GET['aim'])){ $obj=new smsPush($_GET['tel']); $flag=$obj->sendSMS($_GET['aim'],$_GET['text'],$_GET['imgcode']); if($flag) echo '發送成功'; }else{ echo "要這樣發送短信哦~"; echo "http://".$_SERVER['HTTP_HOST']."/send.php?tel=12345567&aim=接收方&text=短信內容&imgcode=";}
5次發送之后獲取圖片驗證碼:
<?phpinclude 'liao.class.php';$sms=new smsPush($_GET['tel']);$sms->getImg();
提交圖片驗證碼并發送短信:
<?phpinclude 'liao.class.php';if(isset($_GET['tel'])&&isset($_GET['text'])&&isset($_GET['aim'])){ $obj=new smsPush($_GET['tel']); $flag=$obj->sendSMS($_GET['aim'],$_GET['text'],$_GET['imgcode']); if($flag) echo '發送成功'; }else{ echo "要這樣發送短信哦~"; echo "http://".$_SERVER['HTTP_HOST']."/send.php?tel=12345567&aim=接收方&text=短信內容&imgcode=圖片驗證碼";}
提交參數的方法自己改改吧,為了測試方便,所以我都用的GET,但用POST更好一些
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答