在好例子网,分享、交流、成长!
您当前所在位置:首页PHP 开发实例PHP语言基础 → 微信小程序发送模板消息(后端为php)

微信小程序发送模板消息(后端为php)

PHP语言基础

下载此实例
  • 开发语言:PHP
  • 实例大小:0.02M
  • 下载次数:22
  • 浏览次数:801
  • 发布时间:2019-03-31
  • 实例类别:PHP语言基础
  • 发 布 人:crazycode
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 微信 php hp 消息 发送

实例介绍

【实例简介】
【实例截图】

【核心代码】

<?php
/**
 * @desc: 微信小程序支付demo
 *
 * 支付参考文档:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_3&index=1
 * 小程序开发参考文档:https://mp.weixin.qq.com/debug/wxadoc/dev/index.html?t=2017621
 *      1、wx.request(OBJECT)参考文档: https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html
 *      2、wx.requestPayment(OBJECT)参考文档: https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-pay.html#wxrequestpaymentobject
 *      3、获取openid参考文档:https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html#wxloginobject
 *
 * @author: jason
 * @since:  2017-08-08 18:59
 */
$ret = array('resultCode' => 1);
$type = isset($_POST['type']) ? trim($_POST['type']) : '';
switch($type){
    case 'openid':
        //小程序的appid和appsecret
        $appid = '****************';
        $appsecret = '*****************';
        $code = isset($_POST['code']) ? trim($_POST['code']) : '';
        if(empty($code)){
            $ret['errMsg'] = '登录凭证code获取失败';
            exit(json_encode($ret));
        }
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$appsecret&js_code=$code&grant_type=authorization_code";

        $json = json_decode(file_get_contents($url));
        if(isset($json->errcode) && $json->errcode){
            $ret['errMsg'] = $json->errcode.', '.$json->errmsg;
            exit(json_encode($ret));
        }
        $openid = $json->openid;

        $ret['resultCode'] = 0;
        $ret['openid'] = $openid;
        exit(json_encode($ret));
        break;
    case 'send':
        //小程序的appid和appsecret
        $appid = 'wx22f0f5085f846137';
        $appsecret = '3e5f684e2948de6f74b487386692940a';
        $access_token = '';

        $openid = isset($_POST['openid']) ? trim($_POST['openid']) : ''; //小程序的openid
        if(empty($openid)){
            $ret['errMsg'] = '却少参数openid';
            exit(json_encode($ret));
        }
        //表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
        $formid = isset($_POST['form_id']) ? trim($_POST['form_id']) : '';
        if(empty($formid)){
            $ret['errMsg'] = '却少参数form_id';
            exit(json_encode($ret));
        }

        //消息模板id
        $temp_id = 'eogEBS2i4VeS2rZfwda7kdkePLTcbmPm-wW1s7A0ky4';
        //获取access_token, 做缓存,expires_in:7200
        generate_token($access_token, $appid, $appsecret);

        $data['touser'] = $openid;
        $data['template_id'] = $temp_id;
        $data['page'] =  'pages/index/index'; //该字段不填则模板无跳转
        $data['form_id'] = $formid;
        $data['data'] = array(
            'keyword1' => array('value' => '小程序模板消息测试'),
            'keyword2' => array('value' => '100元'),
            'keyword3' => array('value' => '支付成功'),
            'keyword4' => array('value' => time() * 1000),
            'keyword5' => array('value' => '微信支付'),
            'keyword6' => array('value' => date('Y-m-d H:i:s')),
        );
        $data['emphasis_keyword'] = 'keyword5.DATA'; //模板需要放大的关键词,不填则默认无放大

        $send_url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access_token;
        $str = request($send_url, 'post', $data);
        $json = json_decode(request($send_url, 'post', $data));
        if(!$json){
            $ret['errMsg'] = $str;
            exit(json_encode($ret));
        }else if(isset($json->errcode) && $json->errcode){
            $ret['errMsg'] = $json->errcode.', '.$json->errmsg;
            exit(json_encode($ret));
        }
        $ret['resultCode'] = 0;
        exit(json_encode($ret));
        break;
    case 'pay':
        include_once('lib/WxPayPubHelper/WxPayPubHelper.php');

        $openid = isset($_POST['openid']) ? trim($_POST['openid']) : '';
        if(empty($openid)){
            $ret['errMsg'] = '缺少参数openid';
            exit(json_encode($ret));
        }

        $order = new UnifiedOrder_pub();
        $order->setParameter("openid", $openid);//商品描述
        $order->setParameter('out_trade_no', 'phpdemo' . $timestamp);
        $order->setParameter('total_fee', 1);
        $order->setParameter('trade_type', 'JSAPI');
        $order->setParameter('body', 'PHP微信小程序支付测试');
        $order->setParameter('notify_url', 'https://www.example.cn/a.php');

        $prepay_id = $order->getPrepayId();
        $jsApi = new JsApi_pub();
        $jsApi->setPrepayId($prepay_id);
        $jsApiParams = json_decode($jsApi->getParameters());

        $ret['resultCode'] = 0;
        $ret['params'] = array(
                'appid' => $jsApiParams->appId,
                'timestamp' => $jsApiParams->timeStamp,
                'nonce_str' => $jsApiParams->nonceStr,
                'sign_type' => $jsApiParams->signType,
                'package' => $jsApiParams->package,
                'pay_sign' => $jsApiParams->paySign,
            );
        exit(json_encode($ret));
        break;
    default :
        $ret['errMsg'] = 'No this type : ' . $type;
        exit(json_encode($ret));
       break;
}

function generate_token(&$access_token, $appid, $appsecret){
    $token_file = '/tmp/token';
    $general_token = true;
    if(file_exists($token_file) && ($info = json_decode(file_get_contents($token_file)))){
        if(time() < $info->create_time   $info->expires_in - 200){
            $general_token = false;
            $access_token = $info->access_token;
        }
    }
    if($general_token){
        new_access_token($access_token, $token_file, $appid, $appsecret);
    }
}

function new_access_token(&$access_token, $token_file, $appid, $appsecret){
    $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
    $str = file_get_contents($token_url);
    $json = json_decode($str);
    if(isset($json->access_token)){
        $access_token = $json->access_token;
        file_put_contents($token_file, json_encode(array('access_token' => $access_token, 'expires_in' => $json->expires_in, 'create_time' => time())));
    }else{
        file_put_contents('/tmp/error', date('Y-m-d H:i:s').'-Get Access Token Error: '.print_r($json, 1).PHP_EOL, FILE_APPEND);
    }
}

function request($url, $method, array $data, $timeout = 30) {
    try {
        $ch = curl_init();
        /*支持SSL 不验证CA根验证*/
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        /*重定向跟随*/
        if (ini_get('open_basedir') == '' && !ini_get('safe_mode')) {
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        }
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

        //设置 CURLINFO_HEADER_OUT 选项之后 curl_getinfo 函数返回的数组将包含 cURL
        //请求的 header 信息。而要看到回应的 header 信息可以在 curl_setopt 中设置
        //CURLOPT_HEADER 选项为 true
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLINFO_HEADER_OUT, false);

        //fail the request if the HTTP code returned is equal to or larger than 400
        //curl_setopt($ch, CURLOPT_FAILONERROR, true);
        $header = array("Content-Type:application/json;charset=utf-8;", "Connection: keep-alive;");
        switch (strtolower($method)) {
            case "post":
                curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
                curl_setopt($ch, CURLOPT_URL, $url);
                break;
            case "put":
                curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
                curl_setopt($ch, CURLOPT_URL, $url);
                break;
            case "delete":
                curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($data));
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
                break;
            case "get":
                curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($data));
                break;
            case "new_get":
                curl_setopt($ch, CURLOPT_URL, $url."?para=".urlencode(json_encode($data)));
                break;
            default:
                throw new Exception('不支持的HTTP方式');
                break;
        }
        $result = curl_exec($ch);
        if (curl_errno($ch) > 0) {
            throw new Exception(curl_error($ch));
        }
        curl_close($ch);
        return $result;
    } catch (Exception $e) {
        return "CURL EXCEPTION: ".$e->getMessage();
    }
}

标签: 微信 php hp 消息 发送

实例下载地址

微信小程序发送模板消息(后端为php)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警