php实现微信推送消息
一.
<?php
namespace Org\Weixin;
class OrderPush
{
protected $appid;
protected $secrect;
protected $accessToken;
function __construct($appid, $secrect)
{
$this->appid = $appid;
$this->secrect = $secrect;
$this->accessToken = $this->getToken($appid, $secrect);
}
/**
* 发送post请求
* @param string $url
* @param string $param
* @return bool|mixed
*/
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch); //运行curl
curl_close($ch);
return $data;
}
/**
* 发送get请求
* @param string $url
* @return bool|mixed
*/
function request_get($url = '')
{
if (empty($url)) {
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* @param $appid
* @param $appsecret
* @return mixed
* 获取token
*/
protected function getToken($appid, $appsecret)
{
if (S($appid)) {
$access_token = S($appid);
} else {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
$token = $this->request_get($url);
$token = json_decode(stripslashes($token));
$arr = json_decode(json_encode($token), true);
$access_token = $arr['access_token'];
S($appid, $access_token, 720);
}
return $access_token;
}
/**
* 发送自定义的模板消息
* @param $touser
* @param $template_id
* @param $url
* @param $data
* @param string $topcolor
* @return bool
*/
public function doSend($touser, $template_id, $url, $data, $topcolor = '#7B68EE')
{
/*
* data=>array(
'first'=>array('value'=>urlencode("您好,您已购买成功"),'color'=>"#743A3A"),
'name'=>array('value'=>urlencode("商品信息:微时代电影票"),'color'=>'#EEEEEE'),
'remark'=>array('value'=>urlencode('永久有效!密码为:1231313'),'color'=>'#FFFFFF'),
)
*/
$template = array(
'touser' => $touser,
'template_id' => $template_id,
'url' => $url,
'topcolor' => $topcolor,
'data' => $data
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->accessToken;
$dataRes = $this->request_post($url, urldecode($json_template));
if ($dataRes['errcode'] == 0) {
return true;
} else {
return false;
}
}
}
原文链接:https://www.jianshu.com/p/f3caa4fd54ad(感谢分享)
二、
public function http_request($url,$data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
public function moban($name, $zu, $remain, $openid)
{
$appid = ""; //填写微信后台的appid
$appsecret = ""; //填写微信后台的appsecret
//从数据库查看access_token
$sql = "SELECT * FROM `tokentime` WHERE id='$appid'";
$query = mysql_query($sql);
$rk = mysql_fetch_array($query);
$time = date('Y-m-d H:i:s', time());
if ($rk == "") //数据库查询无结果 获取access_token并存入
{
$TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
$json = file_get_contents($TOKEN_URL);
$result = json_decode($json, true);
$ACCESS_TOKEN = $result['access_token'];
$sql1 = "INSERT INTO `tokentime` (`id`,`access_token`,`time`) VALUES ('$appid','$ACCESS_TOKEN','$time')";
$query1 = mysql_query($sql1);
} else {
$time_b = $rk['time'];//上次存的时间
$time_n = date('Y-m-d H:i:s', time() - 7200);
if ($rk['access_token'] == "" || $time_b < $time_n) {
$TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
$json = file_get_contents($TOKEN_URL);
$result = json_decode($json, true);
$ACCESS_TOKEN = $result['access_token'];
$sql2 = "UPDATE tokentime SET access_token='$ACCESS_TOKEN',time='$time' WHERE id='$appid'";
$query2 = mysql_query($sql2);
} else {
$ACCESS_TOKEN = $rk['access_token'];
}
}
//模板消息
$times = date('m月d日 H:i:s', time());
$template = array(
'touser' => $openid,
'template_id' => "_0DQerSIqPZaB4vjQjjOIPRXZhcVooFT_390vDhHhVw", //模板的id
'url' => "http://weixin.qq.com/download",
'topcolor' => "#FF0000",
'data' => array(
'name' => array('value' => urlencode($name), 'color' => "#00008B"), //函数传参过来的name
'zu' => array('value' => urlencode($zu), 'color' => '#00008B'), //函数传参过来的zu
'time' => array('value' => urlencode($times), 'color' => '#00008B'), //时间
'remain' => array('value' => urlencode($remain), 'color' => '#00008B'),//函数传参过来的ramain
)
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $ACCESS_TOKEN;
$res = http_request($url, urldecode($json_template));
if ($res[errcode] == 0) echo '消息发送成功!';
}
原文链接:https://yq.aliyun.com/articles/200767?spm=a2c4e.11155472.0.0.2061590cl0G6F2(感谢分享)
php实现微信推送消息的更多相关文章
- java-给微信推送消息 利用企业微信
目的:给关注用户推送消息 场景:自动化测试,运维监控,接口访问等报错预警.例如线上接口报错,发送提醒消息 准备工作: 1:注册企业号(为什么不用公众号呢?) 企业号注册 2:常用参数介绍: 1:COR ...
- 免注册公众号的三种微信推送消息服务的C#代码实现
有时候我们需要监控一些网络上的变化,但是每次去刷新网页却又很麻烦,而且大部分刷新的时候网页并没有更新.那么有没有一个工具,可以监控网页变化,并将变化的结果推送到手机微信上呢? 这里有很多应用场景,比如 ...
- python使用微信推送消息
from wxpy import * #该库主要是用来模拟与对接微信操作的 import requests from datetime import datetime import time impo ...
- node-wechat 微信推送消息
https://github.com/nswbmw/node-wechat/blob/master/index.js
- 小程序推送消息(Template)
最近搞小程序模拟推送消息,才发现小程序推送消息接口准备下线. 请注意,小程序模板消息接口将于2020年1月10日下线,开发者可使用订阅消息功能 咱们现在有需求,所以不管下不下,完成再说. 一:”获取a ...
- 使用ESP8266nodeMCU 向微信推送模板数据
使用HTTPS协议向微信公众号推送消息,(使用ESP8266的低成本实现) 前几天被朋友问到这个东西的实现方式,花了一下午时间研究一下,特此记录.没有排版比较乱. 一丶前往微信公众平台注册微信微信公众 ...
- python 微信推送模板消息
#!/usr/bin/env python #-*- coding: utf-8 -*- import httplib import json import MySQLdb #从数据库中获取acces ...
- node配置微信小程序解密消息以及推送消息
上一篇文章介绍过 微信小程序配置消息推送,没有看过的可以先去查看一下,这里就直接去把那个客服消息接口去解密那个消息了. 在这里我选择的还是json格式的加密. 也就是给小程序客服消息发送的消息都会被微 ...
- $Django 支付宝支付,微信服务号推送消息 (测试需要把应用程序部署到服务器上)
一 支付宝支付 大概 支付宝支付 正式环境:需要用营业执照去申请商户号,appid 测试环境:沙箱环境:https://openhome.alipay.com/platform/appDaily.ht ...
随机推荐
- CSS篇-dispaly、position、定位机制、布局、盒子模型、BFC
display常用值 参考链接英文参考链接中文 // 常用值 none:元素不显示 inline:将元素变为内联元素,默认 block:将元素变为块级元素 inline-block:将元素变为内联块级 ...
- codefoces D. Phoenix and Science
原题链接:https://codeforc.es/problemset/problem/1348/D 题意:给你一个体重为一克的细菌(它可以每天进行一次二分裂即一分为二体重均分:晚上体重增加1克)求最 ...
- 五个问题,三大策略,手把手教你定制App性能监控方案
作者:友盟+U-APM团队 Why? 为什么要做应用性能监控? 首先,我们要知道应用性能监控具体指什么?以及目的: 监控是一套完整的"监视+报警"的系统.对于像我们这样的App开发 ...
- 《C++反汇编与逆向分析技术揭秘》--算术运算和赋值
一.加法 1.Debug下: 14: int nVarOne0 = 1 + 5 - 3 * 6;//编译时计算得到结果 00C0550E C7 45 F8 F4 FF FF FF mov dword ...
- .Net5下WebRequest、WebClient、HttpClient是否还存在使用争议?
WebRequest.WebClient.HttpClient 是C#中常用的三个Http请求的类,时不时也会有人发表对这三个类使用场景的总结,本人是HttpClient 一把梭,也没太关注它们的内部 ...
- java例题_12 奖金问题(暴力破解)
1 /*12 [程序 12 计算奖金] 2 题目:企业发放的奖金根据利润提成. 3 利润I低于或等于 10 万元时,奖金可提 10%: 4 利润高于 10 万元,低于 20 万元时,低于 10 万元的 ...
- 每天自学两小时Python,整理了最详细的学习路线和规
上次这篇文章每天自学两小时Python,三个月学通月入20K主要是给大家整理了学习资料视频和PDF书籍,很多需要的都关注私信领取了. 很多朋友领取之后都问我教程有了那么应该从哪去开始学习呢,私信太多我 ...
- VS2019 自定义项目模板
前言: 使用"宇宙最强IDE"开发项目时,都需要根据不同情况选择一个项目模板,来满足开发需求:如下 VS为我们提供了基础的项目模板,但现有项目模板未包含基础功能如:日志输出.审计日 ...
- mvel 配合正则表达式实现文本替换
mvel 依赖 <dependency> <groupId>org.mvel</groupId> <artifactId>mvel2</artif ...
- 【NCRE】三级网络技术 选择题易错点记录(1)
部分易错点 连接到一个集线器的多个节点不能同时发送数据帧 嵌入式安装插座用来连接双绞线 异步串行端口 PPP 同步串行端口 PPP/HPLC 对于频繁改变位置并使用DHCP获取IP地址的DNS客户端, ...