短信接口示例
主要為大家分享PHP短信接口代碼,PHP短信群發、PHP短信驗證碼發送,感興趣的小伙伴們可以參考一下。
<?php class SendCode{ private $url = 'http://139.196.108.241:8080'; private function post_curls($url, $post) { $curl = curl_init(); // 啟動一個CURL會話 curl_setopt($curl, CURLOPT_URL, $url); // 要訪問的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 對認證證書來源的檢查 curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模擬用戶使用的瀏覽器 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動設置Referer curl_setopt($curl, CURLOPT_POST, 1); // 發送一個常規的Post請求 curl_setopt($curl, CURLOPT_POSTFIELDS, $post); // Post提交的數據包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設置超時限制防止死循環 curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示返回的Header區域內容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以文件流的形式返回 $res = curl_exec($curl); // 執行操作 if (curl_errno($curl)) { echo 'Errno'.curl_error($curl);//捕抓異常 } curl_close($curl); // 關閉CURL會話 return $res; // 返回數據,json格式 } //$account 用戶賬號 //$pswd 必填參數。用戶密碼 //$mobile 必填參數。合法的手機號碼 //$msg 必填參數。短信內容 //$ts 可選參數,時間戳,格式yyyyMMddHHmmss //$state 必填參數 狀態 1:驗證碼短信 2:營銷短信 3:語音驗證碼 public function send($account,$pswd,$mobile,$msg,$ts,$state){ if($ts != ''){ $pswd = md5($account.$pswd.$ts); } $url = ''; switch ($state) { case 1: $url = $this->url.'/Api/HttpSendSMYzm.ashx'; break; case 2: $url = $this->url.'/Api/HttpSendSMYx.ashx'; break; case 3: $url = $this->url.'/Api/HttpSendSMVoice.ashx'; break; default: $url = ''; break; } $data = array('account' => $account,'pswd'=>$pswd,'mobile'=>$mobile,'msg'=>$msg,'ts'=>$ts); $huawei_res= $this->post_curls($url,$data); $huawei_res=json_decode($huawei_res,true); return $huawei_res ; } } //$account 用戶賬號 //$pswd 必填參數。用戶密碼 //$mobile 必填參數。合法的手機號碼 //$msg 必填參數。短信內容 //$ts 可選參數,時間戳,格式yyyyMMddHHmmss //$state 必填參數 狀態 1:驗證碼短信 2:營銷短信 3:語音驗證碼 $send = new SendCode(); $re = $send->send('您的賬號','您的密碼','手機號','短信內容',time(),1); print_r($re);