首页 > 编程语言 >PHP聚合短信

PHP聚合短信

时间:2023-07-07 12:12:28浏览次数:32  
标签:ch 聚合 setopt return code 短信 curl PHP CURLOPT

<?php

namespace app\service;

use fast\Random;
use think\Exception;
use think\Log;

class SmsService extends BaseService
{
    public $key = '';//您申请的接口调用Key
    public $template_id = '';//模板id


    public function send(string $mobile, string $event, string $ip)
    {
        $code = Random::numeric(config('captcha.length'));
        // 请求参数
        $smsConf = array(
            'key' => $this->key, //您申请的APPKEY
            'mobile' => $mobile, //接受短信的用户手机号码
            'tpl_id' => $this->template_id, //您申请的短信模板ID,根据实际情况修改
            'tpl_value' => '#code#=' . $code //您设置的模板变量,根据实际情况修改
        );
        $content = self::sendCodeFunc('http://v.juhe.cn/sms/send', $smsConf, 1); //请求发送短信
        $res = json_decode($content, true);
        $error_code = $res['error_code'];
        if ($error_code == 0) {
//           发送成功
            $sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => time()]);
        }else{
            Log::log('短信发送出错,手机号码:'.$mobile.',msg:'.$res['reason']);
            return false;
        }
        return true;
    }

    /**
     * 发起网络请求函数
     * @param string $url 请求的URL
     * @param bool $params 请求的参数内容
     * @param int $ispost 是否POST请求
     * @return bool|string 返回内容
     */
    public static function sendCodeFunc($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, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
        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 . '?' . $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;
    }


}

 

标签:ch,聚合,setopt,return,code,短信,curl,PHP,CURLOPT
From: https://www.cnblogs.com/PLasir/p/17534597.html

相关文章

  • mac安装php单点调试环境
    页面预览和抓包方式sudophp-fpmsudokillallphp-fpm开启/usr/local/etc/nginx/nginxnginx-squit运行Charles就行 单点调试环境方式:配置apache运行的目录,然后+phpStorm的xdebug就行,postman发请求带xdebug生成的keysudoapachectlstart浏览器debug打开,phpStorm配......
  • php-fpm服务器负载均衡配置
    前面,我们配置了负载均衡的网页转发,发现只是访问做了均衡转发,但是没有解决我们的实际问题,我们的瓶颈是fpm服务器出现了处理不了的情况seembusy,youmustincresestart_serversormax_children.运行一段时间后,发现仍然有处理不了的请求,很明显没有解决问题。所以这次,我们将fpm......
  • php代码部署到独立主机(服务器)
    一,访问主机ssh二,上传文件到主机(服务器) 部署php文件 ......
  • PHP应用出现500 : The page cannot be displayed because an internal server error h
    问题描述PHP应用突然遇见了500 Thepagecannotbedisplayedbecauseaninternalservererrorhasoccurred.错误,但是如果访问一个静态HTML页面,就可以成功。只要是PHP页面,就是500。 问题解答登录AppService的Kudu站点,查看日志发现一句:  scriptProcessorcouldnotbefoun......
  • 【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed
    问题描述PHP应用突然遇见了500 Thepagecannotbedisplayedbecauseaninternalservererrorhasoccurred.错误,但是如果访问一个静态HTML页面,就可以成功。只要是PHP页面,就是500。 问题解答登录AppService的Kudu站点,查看日志发现一句:  scriptProcessorcouldnotb......
  • thinkphp6 nginx配置
    server{listen88;server_namelocalhost;root/www/tp/public;indexindex.phpindex.htmlindex.htm;charsetutf-8;location/{if(!-e$request_filename){rewrite^/index.php(.*)$/index.php?=s......
  • php中执行redis的布隆过滤器的代码
    <?php//测试redis布隆过滤器$redis=newRedis();$redis->connect('127.0.0.1',6379);//创建一个容量是1千万,布隆过滤器://$redis->rawCommand('BF.RESERVE','nzc:redisbloom3','0.0001','10000000');//向布隆过滤器中添加元素:/......
  • PHP中的Trait
    PHP实现了一种代码复用的方法,称为trait,Trait是为类似PHP的单继承语言而准备的一种代码复用机制。Trait为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用method。Trait和Class组合的语义定义了一种减少复杂性的方式,避免传统多继承和Mixin......
  • 去除内容多余样式php函数-文章来源 植物号 www.zhiwuhao.com
    去除内容多余样式php函数-文章来源植物号 www.zhiwuhao.comfunctionKillStyle($content){$content=preg_replace("/STYLE=.+?['|\"]/i",'',$content);//去除样式$content=preg_replace("/style=.+?['|\"]/i",'',$conte......
  • ES 聚合查询
    ES聚合查询三种模式分桶聚合(Bucketaggregations)指标聚合(Metricsaggregations)管道聚合(Pipelineaggregations)  1、分桶聚合(Bucketaggregations)分桶聚合类似与关系型数据库的GroupBy查询,按照指定的条件,进行分组统计 图中首先按照手机的品牌进行分桶统计......