近日,短信平臺接口技術人員在進行正常工作維護時,看到一位近期的客戶對本平臺接口進行的實例展現,如下:
<?php
/**
*
* @author "林文發 QQ:80807616"
* 創瑞官網:http://web.cr6868.com/default.aspx
* 作用:發送手機短信
* 日期:2015-04-18
*/
namespace plugin\sms;
class CRSms{
private static $_instance;
public $account='';//后臺帳戶
public $pwd='';//后臺里可以查看
public $sign='簽名';//簽名這個是自動加在短信里的。
public static function getInstance(){
if(self::$_instance==null){
self::$_instance=new self;
}
return self::$_instance;
}
/**
* 發送驗證碼
* @param 手機號 $phone
* @param 驗證碼 $code
* @return string
*/
public function SendCode($phone,$code){
$phone=intval($phone);
$code=intval($code);
if($phone==0)return false;
if($code==0)return false;
$ret=self::getInstance()->SendMsg($phone, "驗證碼({$code})此驗證碼只用于注冊、登錄或者找回密碼,驗證碼提供給他人將導致帳號被盜。");
return $ret;
}
/**
* 發送短信
* @param 手機號 $tel 可以是單個,多個時使用數組批量發送
* @param 要發送的內容 $content
* @param string $type
* @return string
*/
public function SendMsg($tel,$content,$type=''){
$num = 2;
if($type){
$phone=implode(',',$tel);
}else{
$phone=$tel;
}
//$account = '0000';
//$pwd = '000000000D';
$date = '';//date("Y-m-d H:i:s");
//$content = iconv("utf-8","gbk",$content."[簽名]");
//$content = urlencode($content);
//$url = "http://web.cr6868.com/asmx/sms ... t%3B.$account."&pwd=".$pwd."&content=".$content."&mobile=".$phone."&stime=".$date."&sign=簽名&type=pt&extno=";
$url = 'http://web.cr6868.com/asmx/smsservice.aspx';
$data['name'] = $this->account;
$data['pwd'] = $this->pwd;
$data['content'] = $content;
$data['mobile'] = $phone;
$data['stime'] = $date;
$data['sign'] = $this->sign;
$data['type'] = 'pt';
$data['extno'] = '';
$info = $this->postSMS($url, $data);
return $info;
}
/**
* POST提交短信數據
*/
protected function postSMS($url,$data=''){
$row = parse_url($url);
$host = $row['host'];
$port = $row['port'] ? $row['port']:80;
$file = $row['path'];
while (list($k,$v) = each($data)){
$post .= rawurlencode($k)."=".rawurlencode($v)."&"; //轉URL標準碼
}
$post = substr( $post , 0 , -1 );
$len = strlen($post);
$fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
if (!$fp) {
return "$errstr ($errno)\n";
} else {
$receive = '';
$out = "POST $file HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Content-type: application/x-www-form-urlencoded\r\n";
$out .= "Connection: Close\r\n";
$out .= "Content-Length: $len\r\n\r\n";
$out .= $post;
fwrite($fp, $out);
while (!feof($fp)) {
$receive .= fgets($fp, 128);
}
fclose($fp);
$receive = explode("\r\n\r\n",$receive);
unset($receive[0]);
return implode("",$receive);
}
}
}
<?php
//使用例子
use plugin\sms;
CRSms::getInstance->SendCode();
CRSms::getInstance->SendMsg();
?>
本實例為php語言實例,創瑞短信平臺在此予以大家分享與借鑒,同時感謝千縱網絡科技提供的相關實例代碼!