快乐每一天

自己选择的路,跪着也要走下去...

基于ThinkPHP5封装的 微信公众号模板消息发送(第二版)

// 获取TOKEN,一天就2000条,用一次少一次,跟获取用户信息的access_token 不一样。
    public static function getToken(){
        $appid = config('appid');
        $secret = config('secret');
        $urla = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .$appid . "&secret=" . $secret;
        $outputa = self::curlGet($urla);
        $result = json_decode($outputa, true);
        return $result['access_token'];
    }

    /**
     * 发送get请求
     * @param string $url 链接
     * @return bool|mixed
     */
    private static function curlGet($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        if(curl_errno($curl)){
            return 'ERROR ' . curl_error($curl);
        }
        curl_close($curl);
        return $output;
    }

    /**
     * 发送post请求
     * @param string $url 链接
     * @param string $data 数据
     * @return bool|mixed
     */
    private static function curlPost($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if(!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /**
     * pushMessage 发送自定义的模板消息
     * @param  array  $data          模板数据
        $data = [
            'openid' => '', 用户openid
            'url' => '', 跳转链接
            'template_id' => '', 模板id
            'data' => [ // 消息模板数据
                'first'    => ['value' => urlencode('黄旭辉'),'color' => "#743A3A"],
                'keyword1' => ['value' => urlencode('男'),'color'=>'blue'],
                'keyword2' => ['value' => urlencode('1993-10-23'),'color' => 'blue'],
                'remark'   => ['value' => urlencode('我的模板'),'color' => '#743A3A']
            ]
        ];
     * @param  string $topcolor 模板内容字体颜色,不填默认为黑色
     * @return array
     */
    public static function pushMessage($data = [],$topcolor = '#0000'){
        $template = [
            'touser'      => $data['openid'],
            'template_id' => $data['template_id'],
            'url'         => $data['url'],
            'topcolor'    => $topcolor,
            'data'        => $data['data']
        ];
        $json_template = json_encode($template);
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . self::getToken();
        $result = self::curlPost($url, urldecode($json_template));
        $resultData = json_decode($result, true);
        return $resultData;
    }

    /*我的openid: oIMlh5g0X1KSVIinZk4yhu31N7xs*/

    // 模板  1    g-HwvvOhxt5uAjHjb4li0uDPSpacNUnJePm6bvHEAEc    测试模板    {{first.DATA}} 姓名:{{toName.DATA}} 礼物:{{gift.DATA}} 时间:{{time.DATA}} {{remark.DATA}}    

    public function sendMsg(){
        $weiwei_token=self::getToken();
        $data=[
            'openid' => 'oIMlh5gBzdhUOYEdYcc3WRObs-uQ', // 用户openid
            // 'openid' => 'oIMlh5g0X1KSVIinZk4yhu31N7xs', // 用户openid
            'access_token' => $weiwei_token,
            'template_id' => "g-HwvvOhxt5uAjHjb4li0uDPSpacNUnJePm6bvHEAEc", // 填写你自己的消息模板ID
            'data' => [ // 模板消息内容,根据模板详情进行设置
                'first'    => ['value' => urlencode("尊敬的某某某先生,您好,您太帅了"),'color' => "#743A3A"],
                'toName' => ['value' => urlencode("2019必胜猪"),'color'=>'blue'],
                'gift' => ['value' => urlencode("2019必胜猪"),'color'=>'blue'],
                'time' => ['value' => urlencode("2019必胜猪"),'color' => 'green'],
                'remark'   => ['value' => urlencode("请收下我的膝盖"),'color' => '#743A3A']
            ],
            'url'=>'http://cssnb.com'
        ];
        // 'url_link' => 'http://cssnb.com' // 消息跳转链接
        echo "<pre>";
        var_dump(self::pushMessage($data));
        echo "</pre>";
        echo "<hr>";
        echo "<pre>";
        var_dump($data);
        echo "</pre>";

    }

    /*执行
    http://yycard.cssnb.com/index/test/sendMsg

    返回结果:
    array(3) {
      ["errcode"]=>
      int(0)
      ["errmsg"]=>
      string(2) "ok"
      ["msgid"]=>
      int(774234127473180673)
    }
    array(5) {
      ["openid"]=>
      string(28) "oIMlh5g0X1KSVIinZk4yhu31N7xs"
      ["access_token"]=>
      string(157) "20_TwEAN5XflLOjPEDCx9F2RUnxvkZHDIVQYoK206TVq_gdoaH7EeFJ_Am2DM_IyaJnQ3q7qK2PHVI1hSKogPXA2h1k2K7_7QCXxL6pUsAejCK-s-yKApLYd97VnfhwLjc7tndKmCAVDayLgogdQVMbAAAUZQ"
      ["template_id"]=>
      string(43) "g-HwvvOhxt5uAjHjb4li0uDPSpacNUnJePm6bvHEAEc"
      ["data"]=>
      array(5) {
        ["first"]=>
        array(2) {
          ["value"]=>
          string(144) "%E5%B0%8A%E6%95%AC%E7%9A%84%E6%9F%90%E6%9F%90%E6%9F%90%E5%85%88%E7%94%9F%EF%BC%8C%E6%82%A8%E5%A5%BD%EF%BC%8C%E6%82%A8%E5%A4%AA%E5%B8%85%E4%BA%86"
          ["color"]=>
          string(7) "#743A3A"
        }
        ["keyword1"]=>
        array(2) {
          ["value"]=>
          string(31) "2019%E5%BF%85%E8%83%9C%E7%8C%AA"
          ["color"]=>
          string(4) "blue"
        }
        ["keyword2"]=>
        array(2) {
          ["value"]=>
          string(31) "2019%E5%BF%85%E8%83%9C%E7%8C%AA"
          ["color"]=>
          string(4) "blue"
        }
        ["keyword3"]=>
        array(2) {
          ["value"]=>
          string(31) "2019%E5%BF%85%E8%83%9C%E7%8C%AA"
          ["color"]=>
          string(5) "green"
        }
        ["remark"]=>
        array(2) {
          ["value"]=>
          string(63) "%E8%AF%B7%E6%94%B6%E4%B8%8B%E6%88%91%E7%9A%84%E8%86%9D%E7%9B%96"
          ["color"]=>
          string(7) "#743A3A"
        }
      }
      ["url"]=>
      string(16) "http://cssnb.com"
    }
    */
此文章共有条评论, 人参与 |Powerd By Angboo