1. 程式人生 > >【PHP】創藍253雲通訊paas平臺短信驗證碼接口調用demo

【PHP】創藍253雲通訊paas平臺短信驗證碼接口調用demo

PHP 創藍253 短信驗證碼 數據接口

<?php
header("Content-type:text/html; charset=UTF-8");

class ChuanglanSmsApi {

    //發送短信的接口地址
    const API_SEND_URL=‘http://sms.253.com/msg/send?‘;

    //查詢余額的接口地址
    const API_BALANCE_QUERY_URL=‘http://sms.253.com/msg/balance?‘;

    const API_ACCOUNT=‘*******‘;//短信賬號從 https://zz.253.com/site/login.html 裏面獲取。

    const API_PASSWORD=‘*******‘;//短信密碼從 from https://zz.253.com/site/login.html 裏面獲取。

    /**
     * 發送短信需要的接口參數
     *
     * @param string $mobile                 手機號碼
     * @param string $msg                         想要發送的短信內容
     * @param string $needstatus         是否需要狀態報告 ‘1‘為需要 ‘0‘位不需要。
     */
    public function sendSMS( $mobile, $msg, $needstatus = 1) {

            //發送短信的接口參數
            $postArr = array (
                                      ‘un‘ => self::API_ACCOUNT,
                                      ‘pw‘ => self::API_PASSWORD,
                                      ‘msg‘ => $msg,
                                      ‘phone‘ => $mobile,
                                      ‘rd‘ => $needstatus
                 );

            $result = $this->curlPost( self::API_SEND_URL , $postArr);
            return $result;
    }

    /**
     * 
     *
     *  查詢余額
     */
    public function queryBalance() {

            // 查詢接口參數
            $postArr = array ( 
                      ‘un‘ => self::API_ACCOUNT,
                      ‘pw‘ => self::API_PASSWORD,
            );
            $result = $this->curlPost(self::API_BALANCE_QUERY_URL, $postArr);
            return $result;
    }

    /**
     * 處理接口返回值
     * 
     */
    public function execResult($result){
            $result=preg_split("/[,\r\n]/",$result);
            return $result;
    }

    /**
     * @param string $url  
     * @param array $postFields 
     * @return mixed
     */
    private function curlPost($url,$postFields){
            $postFields = http_build_query($postFields); 
            if(function_exists(‘curl_init‘)){

                    $ch = curl_init ();
                    curl_setopt ( $ch, CURLOPT_POST, 1 );
                    curl_setopt ( $ch, CURLOPT_HEADER, 0 );
                    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
                    curl_setopt ( $ch, CURLOPT_URL, $url );
                    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
                    $result = curl_exec ( $ch );
                    if(curl_errno($ch))
                    {
                            return ‘Curl error: ‘ . curl_error($ch);
                    }
                    curl_close ( $ch );
            }elseif(function_exists(‘file_get_contents‘)){

                    $result=file_get_contents($url.$postFields);

            }
            return $result;
    }

    //魔術獲取
    public function __get($name){
            return $this->$name;
    }

    //魔術設置
    public function __set($name,$value){
            $this->$name=$value;
    }

}
?>

【PHP】創藍253雲通訊paas平臺短信驗證碼接口調用demo