搜索
首页后端开发php教程聚合数据短信API服务接口PHP请求示例(附源码)

使用前你需要:

①:通过http://www.juhe.cn/docs/api/id/54 申请短信API服务

②:在短信模板中心,添加一个模板,并通过审核

一、聚合数据(www.juhe.cn)短信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));
    }
 
 
 
}

二、调用示例

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

三、成功时返回status为1

7efbe1dbd45fec5a65546ab8bfe605b4.png

更多PHP相关知识,请访问PHP中文网

以上是聚合数据短信API服务接口PHP请求示例(附源码)的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:www.whmblog.cn。如有侵权,请联系admin@php.cn删除
高流量网站的PHP性能调整高流量网站的PHP性能调整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依赖注入:初学者的代码示例PHP中的依赖注入:初学者的代码示例May 14, 2025 am 12:08 AM

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

PHP性能:是否可以优化应用程序?PHP性能:是否可以优化应用程序?May 14, 2025 am 12:04 AM

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

PHP性能优化:最终指南PHP性能优化:最终指南May 14, 2025 am 12:02 AM

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具