$sms = new SMS(); $result = $sms->sendMsg($mobile); if ($result['result'] == "0") { $array['status'] = 0; $array['comment'] = '發送成功...'; $array['data'] = $result; }else { $array['status'] = 1; $array['comment'] = '發送失敗...'; }
SMS.class.php
class SMS extends Controller { // 過期時間為60s private $expire = 60; // 發送驗證碼 public function sendMsg($mobile) { $sms = new m5c_sms(); $content = '【XX公司】您的驗證碼是:'; // 驗證碼 $code = $this->getVerifyCode(); // 發送請求 $content .= $code; $result = $sms->send($mobile, $content); if (strpos($result, "success") > -1) { //提交成功 $array['result'] = "0"; $array['verifymobile'] = $mobile; $array['verifycode'] = $code; // 驗證碼的過期時間點:60秒以后 $verifycodeexpire = time() + $this->expire; $array['verifycodeexpire'] = "$verifycodeexpire"; } else { //提交失敗 $array['result'] = "1"; } return $array; } /** * 獲取6位驗證碼 */ private function getVerifyCode() { $authnum = ''; srand((double)microtime()*1000000);//create a random number feed. $ychar="0,1,2,3,4,5,6,7,8,9"; $list=explode(",",$ychar); for($i=0;$i<6;$i++){ $randnum =rand(0,9); // 10+26; $authnum .= $list[$randnum]; } return $authnum; } /** * 返回保存的驗證碼和過期時間點 */ public function getCodeExpire() { return Array( cookie('verify-code'), cookie('verify-code-expire'), cookie('verify-phone') ); }
m5c_sms.class.php
class m5c_sms { /* * 亂碼問題解決方案,1、GBK編碼提交的首先urlencode短信內容(content),然后在API請求時,帶入encode=gbk 2、UTF-8編碼的將content 做urlencode編碼后,帶入encode=utf8或utf-8 實例:http://m.5c.com.cn/api/send/index.php?username=XXX&password_md5=XXX&apikey=XXX&mobile=XXX&content=%E4%BD%A0%E5%A5%BD%E6%89%8D%E6%94%B6%E7%9B%8A%E9%9F%A6&encode=utf8 * * 關於內容轉碼問題。 UTF-8 轉 GBK:$content = iconv("UTF-8","GBK//IGNORE",$content);GBK 轉 UTF-8:$content = iconv("GBK","UTF-8",$content); * * username 用戶名 * password_md5 密碼 * mobile 手機號 * apikey apikey秘鑰 * content 短信內容 * startTime UNIX時間戳,不寫為立刻發送,http://tool.chinaz.com/Tools/unixtime.aspx (UNIX時間戳網站) * * success:msgid 提交成功。 error:msgid 提交失敗 error:Missing username 用戶名為空 error:Missing password 密碼為空 error:Missing apikey APIKEY為空 error:Missing recipient 手機號碼為空 error:Missing message content 短信內容為空 error:Account is blocked 帳號被禁用 error:Unrecognized encoding 編碼未能識別 error:APIKEY or password error APIKEY或密碼錯誤 error:Unauthorized IP address 未授權 IP 地址 error:Account balance is insufficient 余額不足 * */ private $encode = 'UTF-8'; //頁面編碼和短信內容編碼為GBK。重要說明:如提交短信后收到亂碼,請將GBK改為UTF-8測試。如本程序頁面為編碼格式為:ASCII/GB2312/GBK則該處為GBK。如本頁面編碼為UTF-8或需要支持繁體,阿拉伯文等Unicode,請將此處寫為:UTF-8 private $username = 'XXXXXX'; //用戶名 private $password = 'XXXXX'; //private $password_md5 = '1ADBB3178591FD5BB0C248518F39BF6D'; //32位MD5密碼加密,不區分大小寫 private $apikey = 'b20abdab8b1d02ae8f1f5f7a38d6716d'; //apikey秘鑰(請登錄 http://m.5c.com.cn 短信平台-->賬號管理-->我的信息 中復制apikey) public function send($mobile, $content) { //$mobile = '18610310068'; //手機號,只發一個號碼:13800000001。發多個號碼:13800000001,13800000002,...N 。使用半角逗號分隔。 //$content = '您好,您的驗證碼是:12345【美聯】'; //要發送的短信內容,特別注意:簽名必須設置,網頁驗證碼應用需要加添加【圖形識別碼】。 $password_md5 = md5($this->password); $contentUrlEncode = urlencode($content);//執行URLencode編碼 ,$content = urldecode($content);解碼 $result = $this->sendSMS($this->username, $password_md5, $this->apikey, $mobile, $contentUrlEncode, $this->encode); //進行發送 //echo $result; //輸出result內容,查看返回值,成功為success,錯誤為error,(錯誤內容在上面有顯示) /* if (strpos($result, "success")>-1) { //提交成功 //邏輯代碼 } else { //提交失敗 //邏輯代碼 } */ return $result; } //發送接口 private function sendSMS($username, $password_md5, $apikey, $mobile, $contentUrlEncode, $encode) { //發送鏈接(用戶名,密碼,apikey,手機號,內容) $url = "http://m.5c.com.cn/api/send/index.php?"; //如連接超時,可能是您服務器不支持域名解析,請將下面連接中的:【m.5c.com.cn】修改為IP:【115.28.23.78】 $data = array ( 'username' => $username, 'password_md5' => $password_md5, 'apikey' => $apikey, 'mobile' => $mobile, 'content' => $contentUrlEncode, 'encode' => $encode, ); $result = $this->curlSMS($url, $data); return $result; } private function curlSMS($url, $post_fields = array()) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);//用PHP取回的URL地址(值將被作為字符串) curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//使用curl_setopt獲取頁面內容或提交數據,有時候希望返回的內容作為變量存儲,而不是直接輸出,這時候希望返回的內容作為變量 curl_setopt($ch, CURLOPT_TIMEOUT, 30);//30秒超時限制 curl_setopt($ch, CURLOPT_HEADER, 1);//將文件頭輸出直接可見。 curl_setopt($ch, CURLOPT_POST, 1);//設置這個選項為一個零非值,這個post是普通的application/x-www-from-urlencoded類型,多數被HTTP表調用。 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);//post操作的所有數據的字符串。 $data = curl_exec($ch);//抓取URL並把他傳遞給瀏覽器 curl_close($ch);//釋放資源 $res = explode("\r\n\r\n", $data);//explode把他打散成為數組 return $res[2]; //然后在這里返回數組。 }
SMSMessage.class.php
class SMSMessage { // 發送信息 public function sendMsg($mobile, $content) { $sms = new m5c_sms(); $result = $sms->send($mobile, $content); if (strpos($result, "success") > -1) { //提交成功 return array( 'result' => true, 'msg' => $result ); } else { //提交失敗 return array( 'result' => false, 'msg' => $result ); } return $result; }
