+ 收藏我们

网站模板

网站模板搜索
404模板 营销型模板 外贸网站模板 单页模板 双语模板 标签大全
电话:18630701785
首页 > 站长学院 > 基于聚合数据的验证码短信API接口调用示例-PHP版 >

基于聚合数据的验证码短信API接口调用示例-PHP版

时间:2024-04-11 09:34:57

前期准备
根据运营商的要求,目前此接口只对实名认证的企业用户开放使用,所以在使用之前请确保您是实名认证的企业用户

申请接口,你可以在个人中心 ➡️ 数据中心 ➡️ 我的API 模块看到此接口的调用凭证请求key

购买数据的请求次数(免费和有赠送次数的接口可以先行调试)

您必须按照文档提供的接口申请模板后,待客服审核通过后才能调用接口

特别说明
请仔细阅读官网的接口文档,这是聚合数据与开发者的约定,它将有助于您对接口业务的理解,从而顺利地开展开发工作

本示例的侧重点,是帮助开发者顺利获取到接口的响应数据,对于开发者的数据处理等业务逻辑,本文不会展开讨论

本示例旨在最大程度简化开发者的调用步骤,没有将功能模块封装为独立的工具类,方便开发者一键复制后直接运行调试

由于水平能力所限,示例中难免存在错误和疏漏,如有发现还请大家批评指正

参数说明
模板申请接口参数:

参数名    必填    说明
signature    true    模板签名(长度为2-16个中文字符),比如:公司名、产品名称
key    true    申请的请求key
tplcode    true    可供选择的模板id
短信发送接口参数:

参数名    必填    说明
mobile    true    手机号
tpl_id    true    模板id
key    true    申请的请求key
tpl_value    false    模板变量,根据模板中变量决定,可为空
全部代码
模板申请接口请求示例
  <?PHP
    // 请求的接口URL
    $apiUrl = 'Http://v.juhe.cn/vercodesms/submitTpl.php?';
    // 请求参数
    $params = [
        // 模板签名
        'signature' => '模板签名(长度为2-16个中文字符),比如:公司名、产品名称',
        // 您申请的接口调用Key
        'key' => '您申请的接口调用Key',
        //发送的手机号
        'tplcode' => '可供选择的模板id',
    ];
    $paramsString = http_build_query($params);
    
    // 发起接口网络请求
    $response = null;
    try {
        $response = juheHttpRequest($apiUrl, $paramsString, 1);
    } catch (Exception $e) {
        var_dump($e);
        //此处根据自己的需求进行自身的异常处理
    }
    if (!$response) {
        echo "请求异常" . PHP_EOL;
    }
    $result = JSON_decode($response, true);
    if (!$result) {
        echo "请求异常" . PHP_EOL;
    }
    $errorCode = $result['error_code'];
    if ($errorCode === 0) {
        $data = $result['result'];
        echo "您申请的模板id:{$data["tplId"]}" . PHP_EOL;
    } else {
        // 请求异常
        echo "请求异常:{$errorCode}_{$result["reason"]}" . PHP_EOL;
    }
    
    
    function juheHttpRequest($url, $params = false, $ispost = 0)
    {
        $httpInfo = [];
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (windows NT 10.0; WOW64) AppleWEBKit/537.36 (Khtml, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($ch, CURLOPT_TIMEOUT, 12);
        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) {
            // echo "cURL Error: ".curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }
短信发送接口请求示例
    <?php
    // 请求的接口URL
    $apiUrl = 'http://v.juhe.cn/vercodesms/send?';
    // 请求参数
    $params = [
        // 模板id
        'tplId' => '模板id',
        // 您申请的接口调用Key
        'key' => '您申请的接口调用Key',
        //发送的手机号
        'mobile' => '发送的手机号',
        //结合自己的模板中的变量进行设置,如果没有变量,可以删除此参数
        'tplValue' => urlencode('#total#=1000&#used#=100&#balance#=900'),
    ];
    $paramsString = http_build_query($params);
    
    // 发起接口网络请求
    $response = null;
    try {
        $response = juheHttpRequest($apiUrl, $paramsString, 1);
    } catch (Exception $e) {
        var_dump($e);
        //此处根据自己的需求进行自身的异常处理
    }
    if (!$response) {
        echo "请求异常" . PHP_EOL;
    }
    $result = json_decode($response, true);
    if (!$result) {
        echo "请求异常" . PHP_EOL;
    }
    $errorCode = $result['error_code'];
    if ($errorCode === 0) {
        $data = $result['result'];
        echo "请求唯一标示:{$data["sid"]}" . PHP_EOL;
    } else {
        // 请求异常
        echo "请求异常:{$errorCode}_{$result["reason"]}" . PHP_EOL;
    }
    
    
    function juheHttpRequest($url, $params = false, $ispost = 0)
    {
        $httpInfo = [];
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($ch, CURLOPT_TIMEOUT, 12);
        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) {
            // echo "cURL Error: ".curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }

有问题可以加入网站技术QQ群一起交流学习

本站会员学习、解决问题QQ群(691961965)

客服微信号:lpf010888

pbootcms教程

织梦教程

站长学院

SEO

wordpress

竞价教程

信息流

Title