衆所周知,小豬CMS已是目前php軟體市場上的領跑者了,他以功能強大,操作界面友好而著稱,使用者群體非常廣。他使用了Thinkphp架構作為底層,是以在進行功能擴充和二次開發時都是相當容易的。今天我就來帶大家來做一次替換短信接口的工作。
短信接口使用的是短信寶短信平台的短信接口,小夥伴一定會問為什麼使用短信寶作為案例呢?原因很簡單,因為短信寶的平台極其穩定,而且短信發送速度相當快捷,驗證碼和訂單通知在3~5秒就能收到,使用者體驗非常好,是以我們公司一直和短信寶保持着合作關系,小夥伴們也可以去短信寶的官網(http://www.smsbao.com)注冊一個賬号,還有免費的短信條數送呢。
接下來我就來說一下開發步驟:
1. 先打開項目/Conf/sms.php檔案,如果該檔案不存在,則自己建立一個。在檔案中配置短信寶參數:
<?php
return array (
'sms_url' => 'http://api.smsbao.com/sms',
'sms_user' => 'smsbao',
'sms_key' => 'key6523',
'sms_price' => '8',
'sms_sign' => 'pigcms',
'sms_mp' => '13888888888',
'reg_mp_verify' => '0',
);
2.打開項目/PigCms/Lib/ORG/Sms.class.php檔案,找到第35行的sendSms方法,修改方法内的短信發送接口參數,改成短信寶的接口參數。
public function sendSms($token, $content='',$mobile='', $send_time='', $charset='utf-8',$id_code = '') {
$companyid=;
if(!(strpos($token,'_') === FALSE)){
$sarr=explode('_',$token);
$token=$sarr[];
$companyid=intval($sarr[]);
}
if (!$mobile){
$companyWhere=array();
$companyWhere['token']=$token;
if ($companyid){
$companyWhere['id']=$companyid;
}
$company=M('Company')->where($companyWhere)->find();
$mobile=$company['mp'];
}
$thisWxUser=M('Wxuser')->where(array('token'=>Sms::_safe_replace($token)))->find();
$thisUser=M('Users')->where(array('id'=>$thisWxUser['uid']))->find();
if ($token=='admin'){
$thisUser=array('id'=>);
$thisWxUser=array('uid'=>,'token'=>$this->token);
}
if(is_array($mobile)){
$mobile = implode(",", $mobile);
}
$content = Sms::_safe_replace($content);
$data = array(
'topdomain' => C('server_topdomain'),
'u' => C('sms_user'),
'p' =>C('sms_key') ,
'token' => $token,
'c' => $content,
'm'=>$mobile,
'mobileids'=>'11'
);
$post = '';
foreach($data as $k=>$v) {
$post .= $k.'='.$v.'&';
}
// 短信寶接口配置
$smsapi_senturl="http://api.smsbao.com/sms";
$uid =C('sms_user');
$pwd =C('sms_key') ;
$mobileids=C('mobileids');
$return=Sms::send_SMS($smsapi_senturl,$uid,$pwd,$mobile,$content,$mobileids);
$arr = explode('&',$return);
$sta=explode("=",$arr[]);
$this->statuscode = $sta[];
//增加到本地資料庫
$row=array('uid'=>$thisUser['id'],'token'=>$thisWxUser['token'],'time'=>time(),'mp'=>$mobile,'text'=>$content,'status'=>$this->statuscode,'price'=>C('sms_price'));
M('Sms_record')->add($row);
if (intval($this->statuscode)==&&$token!='admin'){
M('Users')->where(array('id'=>$thisWxUser['uid']))->setDec('smscount');
}
return $return;
}
3.最後我們修改一下背景配置界面檔案。打開項目/tpl/System/Site/sms.html,修改為如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>短信接口</title>
<link href="{pigcms::RES}/images/style1.css" type="text/css" rel="stylesheet">
<link href="{pigcms::RES}/images/jquery-1.7.2.min.js" type="text/css" rel="stylesheet">
<link href="{pigcms::RES}/images/jquery.form.js" type="text/css" rel="stylesheet">
<meta http-equiv="x-ua-compatible" content="ie=7" />
</head>
<body class="warp">
<include file="Site:public" />
<script>
function sendtest(){
location.href="{pigcms::U('Site/smssendtest')}&mp="+document.getElementById('sms_mp').value;
}
</script>
<div id="artlist">
<div id="btl">
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="addn">
<form id="myform" action="{pigcms::U('Site/insert')}" method="post">
<tr>
<td height="38" align="right"><strong>共售出:</strong></td>
<td>{pigcms:$total|default='0'}條
</td>
</tr>
<tr>
<td height="38" align="right"><strong>接口位址:</strong></td>
<td><input type="text" name="sms_url" value="{pigcms::C('sms_url')}" class="ipt" size="45" /><span> 可不用填寫</span>
</td>
</tr>
<tr>
<td height="38" align="right"><strong>使用者名:</strong></td>
<td><input type="text" name="sms_user" value="{pigcms::C('sms_user')}" class="ipt" size="45" /><span> </span><span> <a href="http://www.smsbao.com">短信寶注冊</a></span>
</td>
</tr>
<tr>
<td height="38" align="right"><strong>短信key:</strong></td>
<td><input type="text" name="sms_key" value="{pigcms::C('sms_key')}" class="ipt" size="45" />
</tr>
<tr>
<td height="38" align="right"><strong>價格:</strong></td>
<td><input type="text" name="sms_price" value="{pigcms::C('sms_price')}" class="ipt" size="45" /><span> 每條多少分錢(賣給客戶的) </span>
</tr>
<tr>
<td height="38" align="right"><strong>簽名:</strong></td>
<td><input type="text" name="sms_sign" value="{pigcms::C('sms_sign')}" class="ipt" size="45" /><span> 短信的字尾,必須帶有簽名</span>
</tr>
<tr>
<td height="38" align="right"><strong>測試:</strong></td>
<td><input type="text" name="sms_mp" value="{pigcms::C('sms_mp')}" id="sms_mp" class="ipt" size="45" /><span> 輸入手機号以後,然後<a href="###" onclick="sendtest()" style="color:red">點選這裡</a></span>進行測試
</td>
</tr>
<input type="hidden" name="reg_mp_verify" value="0" id="regOff" />
<input type="hidden" name="files" value="<?php echo base64_encode('sms.php');?>" />
<input type="hidden" name="files_hash" value="<?php echo md5('sms.php|validate_pigcms');?>" />
<tr>
<td height="38" colspan="2">
<div id="addkey"></div>
<div style="padding-left:100px;">
<input type="submit" value="儲存設定" id="inpts"/>
</div>
</td>
</tr>
</form>
</table>
</div><br />
<br />
<br />
</div>
</body>
</html>
好了,短信寶接口替換全部完成。。。隻需要修改3個檔案,就可以完成整個修改過程,是不是很簡單呀。另外,在短信寶官網中也提供了插件下載下傳位址:http://www.smsbao.com/plugin/107.html 小夥伴們可以對着短信寶短信插件,來看我這篇文章,這樣會更直覺一些。