php调用云片网接口发送短信实例

本文介绍了一种通过云片网API实现短信发送的方法。利用PHP脚本进行短信内容构造及发送操作,详细展示了curl请求过程及参数设置,并记录了返回结果的日志。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

云片网发送短信

/**
     * [SendSms description]
     * @param [string] $tpl_content [发送的短信内容]
     * @param          $send_mobile [发送的手机号码]
     */
    public function SendSms($tpl_content,$send_mobile)
    {
        $this->yunpian_appkey = env('yunpian_appkey');//云片账户appkey
        $this->yunpian_secret = env('yunpianSecretXYZ');//云片账户secret
        //初始化
        $ch       = curl_init();
        $data=array('text'=>$tpl_content,'apikey'=>$this->yunpian_appkey,'mobile'=>$send_mobile);
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $send_data = curl_exec($ch);
        curl_close($ch);
        //解析返回结果(json格式字符串)
        $backmassage = json_decode($send_data,true);
        echo '$backmassage ';
        //这里是写在了job里面所以记了个日志可忽略!
        Log::info('smsmessagelog: '.$send_data);
        return [$backmassage,$send_data];
    }

云片短信接口文档:http://www.yunpian.com/api2.0/api-domestic/single_send.html

如果对你有帮助,顶我!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值