注意:如果沒(méi)有購(gòu)買(mǎi)短信,則只用來(lái)測(cè)試短信速度,短信內(nèi)容不可編輯,只可修改驗(yàn)證碼數(shù)字。
測(cè)試操作如圖:
進(jìn)入正題:
使用短信寶,需要一個(gè)SmsBao.class.php文件,這個(gè)文件會(huì)放在Org目錄下,文件內(nèi)容在最下方
調(diào)用代碼:
<?phpnamespace Home\Controller;use Think\Controller;use Org\Sms\SmsBao;class IndexController extends Controller { public function index(){ $sms = new SmsBao("賬號(hào)", "密碼"); $msg = $sms->sendSms("1537539****", "這是一個(gè)測(cè)試短信!"); if($msg['status'] == 0) { echo '發(fā)送成功!'; } else { echo '發(fā)送失??!'; } echo $sms->getBalance(); // 返回剩余條數(shù) } }
訪問(wèn)后會(huì)顯示
登錄短信寶,進(jìn)入請(qǐng)求記錄會(huì)看到
如果從短信寶測(cè)試入口測(cè)試,可以在手機(jī)上看到短信,然后在官網(wǎng)的發(fā)送記錄會(huì)看到內(nèi)容
回復(fù)的話,會(huì)在回復(fù)記錄中看到回復(fù)的內(nèi)容
附加:
SmsBao.class.php 內(nèi)容
<?php// +----------------------------------------------------------------------// | smsbao for thinkPHP// +----------------------------------------------------------------------// | Copyright (c) 2005 http://smsbao.com All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://smsbao.com )// +----------------------------------------------------------------------namespace Org\Sms;/** * SmsBao實(shí)現(xiàn)類(lèi) * @category Think * @package Think * @subpackage Sms */class SmsBao { private $account;//短信包賬戶(hù) private $password;//密碼 private $balance;//短信剩余條數(shù) const sendSmsUrl = "http://api.smsbao.com/sms"; const queryBalanceUrl = "http://api.smsbao.com/query"; /** * 架構(gòu)函數(shù) * @access public * @param string $account 在短信寶注冊(cè)的賬戶(hù)名 * @param string $password 在短信寶注冊(cè)的賬戶(hù)名的密碼 */ public function __construct($account,$password) { if (empty($account) || empty($password)) { E("用戶(hù)名和密碼不可為空!"); } $this->account = $account; $this->password = $password; } /** * 發(fā)送短信函數(shù) * @access public * @param string $mobile 手機(jī)號(hào),多個(gè)手機(jī)號(hào)用英文逗號(hào)分隔 * @param string $content 發(fā)送內(nèi)容 * @return array 返回值為數(shù)組,其中status為0表明發(fā)送成功,其他情況下發(fā)送失敗,失敗原因?yàn)閙sg */ public function sendSms($mobile,$content){ $param['u'] = $this->account; $param['p'] = md5($this->password); $param['m'] = $mobile; $param['c'] = $content; $ret = self::http(self::sendSmsUrl, $param); $data['status'] = $ret; $data['msg'] = $ret == 0 ?'發(fā)送成功' : self::getResult($ret); return $data; } /** * 獲取短信剩余條數(shù)函數(shù) * @access public */ public function getBalance(){ $param['u'] = $this->account; $param['p'] = md5($this->password); $ret = self::http(self::queryBalanceUrl, $param); $retArr = split("\n", $ret); $balanceArr = split(",", $retArr[1]); $this->balance = $retArr[0] == 0 ? $balanceArr[1] : self::getResult($ret); return $this->balance; } /** * 發(fā)送http請(qǐng)求 * @access protected * @param string $url 請(qǐng)求地址 * @param string $param get方式請(qǐng)求內(nèi)容,數(shù)組形式,post方式時(shí)無(wú)效 * * @param string $data post請(qǐng)求方式時(shí)的內(nèi)容,get方式時(shí)無(wú)效 * @param string $method 請(qǐng)求方式,默認(rèn)get */ protected static function http($url, $param, $data = '', $method = 'GET'){ $opts = array( CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, ); /* 根據(jù)請(qǐng)求類(lèi)型設(shè)置特定參數(shù) */ $opts[CURLOPT_URL] = $url . '?' . http_build_query($param); if(strtoupper($method) == 'POST'){ $opts[CURLOPT_POST] = 1; $opts[CURLOPT_POSTFIELDS] = $data; if(is_string($data)){ //發(fā)送JSON數(shù)據(jù) $opts[CURLOPT_HTTPHEADER] = array( 'Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data), ); } } /* 初始化并執(zhí)行curl請(qǐng)求 */ $ch = curl_init(); curl_setopt_array($ch, $opts); $data = curl_exec($ch); $error = curl_error($ch); curl_close($ch); //發(fā)生錯(cuò)誤,拋出異常 if($error) throw new \Exception('請(qǐng)求發(fā)生錯(cuò)誤:' . $error); return $data; } private function getResult($key){ $rst['30'] = '密碼錯(cuò)誤'; $rst['40'] = '賬號(hào)不存在'; $rst['41'] = '余額不足'; $rst['42'] = '帳號(hào)過(guò)期'; $rst['43'] = 'IP地址限制'; $rst['50'] = '內(nèi)容含有敏感詞'; $rst['51'] = '手機(jī)號(hào)碼不正確'; return $rst[$key]; } }
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com