>  기사  >  백엔드 개발  >  집계된 데이터 SMS API 서비스 인터페이스 PHP 요청 예시(소스코드 첨부)

집계된 데이터 SMS API 서비스 인터페이스 PHP 요청 예시(소스코드 첨부)

angryTom
angryTom앞으로
2019-10-14 18:17:124355검색

필요한 사항:

①: http://www.juhe.cn/docs/api/id/54를 통해 SMS API 서비스를 신청합니다.

②: SMS 템플릿 센터에서 템플릿을 추가하고 리뷰

1. 집계된 데이터(www.juhe.cn) SMS API 서비스 인터페이스 PHP 요청 샘플 소스 코드

<?php
 
header(&#39;content-type:text/html;charset=utf-8&#39;);
class SendCode
{
    private $key;
    private $tpl_id;
    public function __construct()
    {
        $this->key = &#39;AppKey&#39;;  // 聚合数据后台的AppKey
        $this->tpl_id = &#39;tpl_id&#39;;    // 申请的短信模板ID,根据实际情况修改短信模板
    }
    public function send($mobile){
 
        if (empty($mobile)) {
            $this->show_json(-4,&#39;手机号不能为空&#39;);
        }
 
        $code = mt_rand(100000,999999);
        $sendUrl = &#39;http://v.juhe.cn/sms/send&#39;; //短信接口的URL
        $smsConf = array(
            &#39;key&#39;   => $this->key, //您申请的APPKEY
            &#39;mobile&#39;    => $mobile, //接受短信的用户手机号码
            &#39;tpl_id&#39;    => $this->tpl_id, //您申请的短信模板ID,根据实际情况修改
            &#39;tpl_value&#39; =>&#39;#code#=&#39;.$code.&#39;&#company#=聚合数据&#39; //您设置的模板变量,根据实际情况修改
        );
 
        $content = $this->juhecurl($sendUrl,$smsConf, 1); //请求发送短信
        if($content){
            $result = json_decode($content,true);
            $error_code = $result[&#39;error_code&#39;];
            if($error_code == 0){
                //状态为0,说明短信发送成功
                $data[&#39;code&#39;] = $code;
                $this->show_json(1, $data);
            }else{
                //状态非0,说明失败
                $msg = $result[&#39;reason&#39;];
                $this->show_json(-3, "短信发送失败(".$error_code."):".$msg);
            }
        }else{
            //返回内容异常,以下可根据业务逻辑自行修改
            $this->show_json(-3, &#39;请求发送短信失败&#39;);
        }
 
    }
 
    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @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 , &#39;Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22&#39; );
        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.&#39;?&#39;.$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(&#39;status&#39; => $status);
 
        if (!is_array($return)) {
            if ($return) {
                $ret[&#39;result&#39;][&#39;message&#39;] = $return;
            }
 
            exit(json_encode($ret));
        } else {
            $ret[&#39;result&#39;] = $return;
        }
 
        exit(json_encode($ret));
    }
 
 
 
}

2. 호출 예시

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

3. 성공 시 반환 상태는 1입니다.

PHP 관련 지식을 더 알고 싶으시면 집계된 데이터 SMS API 서비스 인터페이스 PHP 요청 예시(소스코드 첨부)PHP 중국어 홈페이지를 방문해주세요!

위 내용은 집계된 데이터 SMS API 서비스 인터페이스 PHP 요청 예시(소스코드 첨부)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 www.whmblog.cn에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제