zoukankan      html  css  js  c++  java
  • 发送公众号模板消息

    <?php
    class wxservice
    {
    /**
    * @todo w微信服务号接口地址
    */
    private static $appid = "---------";
    private static $appsecret = "----------------";
    private static $template_id = [
    "1"=>"==========",//服务完成提醒
    "2"=>"============",//派单成功通知
    ];
    private static $token;

    public function __construct (){
    self::gettoken();
    }

    /**
    * @todo 获取access_token
    */
    public static function gettoken(){
    if(!file_exists("./upload/token.txt")){
    fopen("./upload/token.txt", "w+");
    }
    $file = file_get_contents("./upload/token.txt");
    $file = json_decode($file,true);
    if( $file['date']<time() ){

    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".self::$appid."&secret=".self::$appsecret;
    $file = file_get_contents($url);
    $file = json_decode($file,true);
    $file['date'] = time() + $file['expires_in'] - 1200;
    $fp = file_put_contents("./upload/token.txt", json_encode($file));
    }
    self::$token = $file['access_token'];
    }

    public static function send_service($openid,$info = [],$type = 1){
    $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".self::$token;
    $arr = [
    "touser"=>$openid,
    "template_id"=>self::$template_id[$type],
    "url"=>"",
    "miniprogram"=>"",
    "data"=>[
    "first"=>[
    "value"=>"{$info['first']}",
    "color"=>"#173177"
    ],
    "keyword1"=>[
    "value"=>"{$info['keyword1']}",
    "color"=>"#173177"
    ],
    "keyword2"=>[
    "value"=>"{$info['keyword2']}",
    "color"=>"#173177"
    ],
    "keyword3"=>[
    "value"=>"{$info['keyword3']}",
    "color"=>"#173177"
    ],
    "remark"=>[
    "value"=>"{$info['remark']}",
    "color"=>"#173177"
    ],
    ]
    ];
    $msg = self::postcurl($url,json_encode($arr));
    return $msg;

    }

    private static function postcurl($submitUrl,$postData = null)
    {
    //提交菜单
    $curl = curl_init($submitUrl);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//SSL证书认证
    curl_setopt($curl, CURLOPT_HEADER, 0); // 过滤HTTP头
    curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);// 显示输出结果
    curl_setopt($curl,CURLOPT_POST,true); // post传输数据
    curl_setopt($curl,CURLOPT_POSTFIELDS,$postData);// post传输数据
    $responseText = curl_exec($curl);
    if($responseText == false)
    {
    throw new IException(curl_error($curl));
    }
    curl_close($curl);
    return json_decode($responseText,true);
    }
    }

  • 相关阅读:
    《全职高手》的全部职业和职业技能(最新版)
    博客园样式美化
    科幻电影免费百度云分享(Scince-fiction cloud share)
    阅读须知
    Website's Game source code
    #P2010 回文日期 的题解
    #P1909 买铅笔 的题解
    #P4770 [NOI2018]你的名字 的题解
    #1074 骑士问题 的题解
    [C++基本语法:从菜鸟变成大佬系列,就像1,2,3那么简单](七):C++的修饰符
  • 原文地址:https://www.cnblogs.com/zlking/p/11447266.html
Copyright © 2011-2022 走看看