search
HomeBackend DevelopmentPHP TutorialAggregated data SMS API service interface PHP request example (source code attached)

You need to:

①: Apply for SMS API service through http://www.juhe.cn/docs/api/id/54

②: In the SMS template center, add a template and pass the review

1. Aggregated data (www.juhe.cn) SMS API service interface PHP request sample source code

<?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. Calling example

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

3. When successful, the status returned is 1

Aggregated data SMS API service interface PHP request example (source code attached)

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of Aggregated data SMS API service interface PHP request example (source code attached). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:www.whmblog.cn. If there is any infringement, please contact admin@php.cn delete
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools