短信接口API——PHP開發實例 |
技術開發人員在使用短信接口API,總會出現這樣或那樣的問題,如-10,用戶名或密碼錯誤,或者400錯誤,等等,其實創瑞提供的短信接口api其實只要按照我們的接口文檔進行調整即可!就以開發語言PHP為例,創瑞短信平臺技術人員就編寫了一下實例,以供用戶參考。(必要參數已模糊化,還請以最新官網短信接口文檔為準!)
<?php /** * * 短信接口api提交地址:http://web.cr6868.com/asmx/smsservice.aspx * 用途:網站注冊短信驗證碼 * 日期:2015-10-10 */ 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 = 'TEST'; //$pwd = 'EKDNLSL45DKEL8888888'; $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(); ?> |
【返回首頁】 |
上一篇:如何防止短信驗證碼被刷? 下一篇:如何讓短信驗證碼接口更安全 |
更多詳情請搜索: |