溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

聚合數(shù)據(jù)短信API服務(wù)接口PHP請(qǐng)求的案例分析

發(fā)布時(shí)間:2020-11-05 11:46:24 來源:億速云 閱讀:162 作者:小新 欄目:編程語言

這篇文章主要介紹了聚合數(shù)據(jù)短信API服務(wù)接口PHP請(qǐng)求的案例分析,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

使用前你需要:

①:通過http://www.juhe.cn/docs/api/id/54 申請(qǐng)短信API服務(wù)

②:在短信模板中心,添加一個(gè)模板,并通過審核

一、聚合數(shù)據(jù)(www.juhe.cn)短信API服務(wù)接口PHP請(qǐng)求示例源碼

<?php
 
header('content-type:text/html;charset=utf-8');
class SendCode
{
    private $key;
    private $tpl_id;
    public function __construct()
    {
        $this->key = 'AppKey';  // 聚合數(shù)據(jù)后臺(tái)的AppKey
        $this->tpl_id = 'tpl_id';    // 申請(qǐng)的短信模板ID,根據(jù)實(shí)際情況修改短信模板
    }
    public function send($mobile){
 
        if (empty($mobile)) {
            $this->show_json(-4,'手機(jī)號(hào)不能為空');
        }
 
        $code = mt_rand(100000,999999);
        $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
        $smsConf = array(
            'key'   => $this->key, //您申請(qǐng)的APPKEY
            'mobile'    => $mobile, //接受短信的用戶手機(jī)號(hào)碼
            'tpl_id'    => $this->tpl_id, //您申請(qǐng)的短信模板ID,根據(jù)實(shí)際情況修改
            'tpl_value' =>'#code#='.$code.'&#company#=聚合數(shù)據(jù)' //您設(shè)置的模板變量,根據(jù)實(shí)際情況修改
        );
 
        $content = $this->juhecurl($sendUrl,$smsConf, 1); //請(qǐng)求發(fā)送短信
        if($content){
            $result = json_decode($content,true);
            $error_code = $result['error_code'];
            if($error_code == 0){
                //狀態(tài)為0,說明短信發(fā)送成功
                $data['code'] = $code;
                $this->show_json(1, $data);
            }else{
                //狀態(tài)非0,說明失敗
                $msg = $result['reason'];
                $this->show_json(-3, "短信發(fā)送失敗(".$error_code."):".$msg);
            }
        }else{
            //返回內(nèi)容異常,以下可根據(jù)業(yè)務(wù)邏輯自行修改
            $this->show_json(-3, '請(qǐng)求發(fā)送短信失敗');
        }
 
    }
 
    /**
     * 請(qǐng)求接口返回內(nèi)容
     * @param  string $url [請(qǐng)求的URL地址]
     * @param  string $params [請(qǐng)求的參數(shù)]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    public function juhecurl($url,$params=false,$ispost=0){
 
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
 
    }
 
 
    public function show_json($status = 1, $return = NULL) {
 
        $ret = array('status' => $status);
 
        if (!is_array($return)) {
            if ($return) {
                $ret['result']['message'] = $return;
            }
 
            exit(json_encode($ret));
        } else {
            $ret['result'] = $return;
        }
 
        exit(json_encode($ret));
    }
 
 
 
}

二、調(diào)用示例

<?php
$send = new SendCode();
$send->send(15113993183);

三、成功時(shí)返回status為1

聚合數(shù)據(jù)短信API服務(wù)接口PHP請(qǐng)求的案例分析

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享聚合數(shù)據(jù)短信API服務(wù)接口PHP請(qǐng)求的案例分析內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細(xì)的解決方法等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI