只要一点 [提交], 看网页服务器就会出现log记录 cat /var/log/apache2/access.log:
127.0.0.1 - - [29/Aug/2020:23:07:57 -0700] "GET /wx.php?signature=1f4142af46fa98cd30b5a57d3e6b8b14f01ea78f&echostr=7204982403406724733×tamp=1598767677&nonce=1224769556 HTTP/1.1" 404 452 "-" "Mozilla/4.0"
127.0.0.1 - - [29/Aug/2020:23:09:29 -0700] "GET /wx.php?signature=66b48d050fdfee446a4ad94b489fb176ba86d402&echostr=8184609992026876859×tamp=1598767769&nonce=1984465893 HTTP/1.1" 404 452 "-" "Mozilla/4.0"
127.0.0.1 - - [29/Aug/2020:23:09:36 -0700] "GET /wx.php HTTP/1.1" 404 452 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
127.0.0.1 - - [29/Aug/2020:23:11:59 -0700] "GET /wx.php HTTP/1.1" 200 167 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
127.0.0.1 - - [29/Aug/2020:23:12:15 -0700] "GET /wx.php?signature=918f4a236bcfd5f8f2b25369b85050411dda08a2&echostr=525327352018754495×tamp=1598767935&nonce=813200003 HTTP/1.1" 200 186 "-" "Mozilla/4.0"
127.0.0.1 - - [29/Aug/2020:23:20:45 -0700] "GET /wx.php HTTP/1.1" 200 167 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 10_2_1 like Mac OS X) AppleWebKit/602.4.6 (KHTML, like Gecko) Mobile/14D27 MicroMessenger/6.5.5 NetType/WIFI Language/zh_CN"
127.0.0.1 - - [29/Aug/2020:23:25:23 -0700] "GET /wx.php?signature=7aa15240c3f687e0ab49b1934ad983652623ee7e&echostr=4353787308509460447×tamp=1598768723&nonce=473211795 HTTP/1.1" 200 187 "-" "Mozilla/4.0"
只要新建 wx.php, 返回 echostr 即可:
<?php
echo $_GET["echostr"];
?>
这样就会显示右上角 提交成功
离线
然后呢, 然后当然是 启用配置,
启用该配置之后, 关注者微信 给 微信公众号发送的消息都传到了 wx.php
你可以继续观察 apache2 log,
cat /var/log/apache2/access.log:
127.0.0.1 - - [30/Aug/2020:00:13:04 -0700] "POST /wx.php?signature=75752c0a62a02d368d467fdeb0fa2d346ef9eab4×tamp=1598771583&nonce=719528559&openid=oVEjzvmgLIF0oLHYccqGZnYyZ4r4 HTTP/1.1" 200 167 "-" "Mozilla/4.0"
127.0.0.1 - - [30/Aug/2020:00:13:16 -0700] "POST /wx.php?signature=877a7a65284aad0a74b4746fd92efb2e192aa7aa×tamp=1598771596&nonce=665457187&openid=oVEjzvmgLIF0oLHYccqGZnYyZ4r4 HTTP/1.1" 200 167 "-" "Mozilla/4.0"
离线
楼顶那个是强行推送成功消息, 并没有验证签名:
这个是校验签名的 wx.php 代码:
<?php
function wx_log($log_msg)
{
$log_filename = $_SERVER['DOCUMENT_ROOT']."/logs";
if (!file_exists($log_filename))
{
// create directory/folder uploads.
mkdir($log_filename, 0777, true);
}
$log_file_data = $log_filename.'/' . date('Y-m-d') . '.log';
file_put_contents($log_file_data, $log_msg, FILE_APPEND);
file_put_contents($log_file_data, "\n", FILE_APPEND);
}
define(TOKEN, "my_token");
wx_log('sign: ' . $_GET['signature']);
wx_log($_GET['echostr']);
wx_log($_GET['timestamp']);
wx_log($_GET['nonce']);
$signature = $_GET['signature'];//签名
$echostr = $_GET['echostr'];
$timestamp = $_GET['timestamp'];//时间戳
$nonce = $_GET['nonce'];//随机数
$arr = [$timestamp, $nonce, TOKEN];
sort($arr, SORT_STRING);
$str = sha1(implode($arr));//重新计算签名
wx_log($str);
if(($str == $signature) && ($str != ""))//如果签名吻合,那么给微信服务器回推消息
{
echo $echostr;
}
?>
离线
看视频学来的 $GLOBALS[‘HTTP_RAW_POST_DATA’],
结果在我的网页服务器死活不显示,
一开始因为我输错了, 检查100遍之后, 发现还是没错.
网上一搜, 原来这个数组在php7已经不适应了,
新的接口是: $data = file_get_contents('php://input');
离线
cat wx1.php
<?php
function wx_log($log_msg)
{
$log_filename = $_SERVER['DOCUMENT_ROOT']."/logs";
if (!file_exists($log_filename))
{
// create directory/folder uploads.
mkdir($log_filename, 0777, true);
}
$log_file_data = $log_filename.'/' . date('Y-m-d') . '.log';
file_put_contents($log_file_data, $log_msg, FILE_APPEND);
file_put_contents($log_file_data, "\n", FILE_APPEND);
}
define(TOKEN, "my_token");
$data = file_get_contents('php://input');
if(empty($data))
{
//wx_log($GLOBALS['HTTP_RAW_POST_DATA']);
wx_log('sign: ' . $_GET['signature']);
wx_log($_GET['echostr']);
wx_log($_GET['timestamp']);
wx_log($_GET['nonce']);
$signature = $_GET['signature'];//签名
$echostr = $_GET['echostr'];
$timestamp = $_GET['timestamp'];//时间戳
$nonce = $_GET['nonce'];//随机数
$arr = [$timestamp, $nonce, TOKEN];
sort($arr, SORT_STRING);
$str = sha1(implode($arr));//重新计算签名
wx_log($str);
if(($str == $signature) && ($str != ""))//如果签名吻合,那么给微信服务器回推消息
{
echo $echostr;
}
}
else
{
wx_log($data);
$obj = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
wx_log('ToUserName:' . $obj->ToUserName);
wx_log('FromUserName:' . $obj->FromUserName);
wx_log('CreateTime:' . $obj->CreateTime);
wx_log('MsgType:' . $obj->MsgType);
wx_log('Content:' . $obj->Content);
$ToUserName = $obj->ToUserName;
$FromUserName = $obj->FromUserName;
$MessageContent = $obj->Content;
$xml_template = '<xml><ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>';
if($MessageContent == '我是谁')
{
$xml_send = sprintf($xml_template, $FromUserName, $ToUserName, time(), 'text', '你是唯一的自己.');
}
else if($MessageContent == '你是谁')
{
$xml_send = sprintf($xml_template, $FromUserName, $ToUserName, time(), 'text', '我是无所不能的公众号');
}
else
{
$xml_send = sprintf($xml_template, $FromUserName, $ToUserName, time(), 'text', '你的问题我没有办法回答,去问周公吧.');
}
echo $xml_send;
exit;
}
?>
这是一天的工作成果.
离线
感谢楼主分享,
微信公众平台开发还是挺不错的,
在聊天的时候就能把设备控制了。
这个聊天信息是推送到自己的的应用服务器(网页服务器),
比如发送 开灯7,
应用服务器收到就可以给绑定该微信的设备7开灯。
离线
服务器推送交互式消息有时间限制的,只有用户发了一条信息给公众号(2小时内??),服务器才可以给微信推送消息。
服务器推送随时可以推送模板消息,但是模板只能从微信库里面选,很可能找不到合适的模板。
离线
楼上两位说得对, 刚刚搞定了模板消息
代码全部是这个: https://www.jianshu.com/p/2ac8cba649a4
$ACCESS_TOKEN = '';//通过微信获取access_token接口 获取的token
$openid = '';//用户openid
$template_id = '';//配置的模板id
$url = '';//点击模板消息跳转的链接
$template = array(
'touser' => $openid,
'template_id' => $template_id,
'url' => $url,
'data' => array(
'first' => array('value' => '恭喜你购买成功!', 'color' => "#173177"),
'keyword1' => array('value' => '巧克力', 'color' => '#173177'),
'keyword2' => array('value' => '39.8元', 'color' => '#173177'),
'keyword3' => array('value' => '2014年9月22日', 'color' => '#173177'),
'remark' => array('value' => '欢迎再次购买!', 'color' => '#173177'),)
);
$send_template_url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $ACCESS_TOKEN;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $send_template_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($template));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$res = curl_exec($ch);
curl_close($ch);
return $res;
只改了配置, 就能给关注的微信号主动推送模板消息了。
离线
https://segmentfault.com/a/1190000019436016
<?php
header("Content-type: text/html; charset=utf-8");
define("ACCESS_TOKEN", "生成的Access_Token");
//创建菜单
function createMenu($data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".ACCESS_TOKEN);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}
//获取菜单
function getMenu(){
return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".ACCESS_TOKEN);
}
//删除菜单
function deleteMenu(){
return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".ACCESS_TOKEN);
}
$data = '{
"button":[
{
"type":"click",
"name":"首页",
"key":"home"
},
{
"type":"click",
"name":"简介",
"key":"introduct"
},
{
"name":"菜单",
"sub_button":[
{
"type":"click",
"name":"hello word",
"key":"V1001_HELLO_WORLD"
},
{
"type":"click",
"name":"赞一下我们",
"key":"V1001_GOOD"
}]
}]
}';
echo createMenu($data);
给微信公众号设置菜单, 验证成功。
离线
不久的新闻有人微信被封,付出了生命的代价,还不放弃微信。不要过渡依赖微信,要是经营多年的公众号哪天被封搞个鸡儿。
什么平台都不要过渡依赖。那年渣浪被整改,清空了所有用户网盘存的东西,我多年在渣浪积累的无形资产文档都没了。
网友存档
https://info.williamlong.info/2016/04/blog-post_25.html
不要徒方便把自己的无形资产交给平台,搞公众号不如学哇酷站长搞个网站,牢牢拽在自己手里的无形资产。
最近编辑记录 dfherj (2020-08-31 18:23:48)
离线
深表赞同!
不久的新闻有人微信被封,付出了生命的代价,还不放弃微信。不要过渡依赖微信,要是经营多年的公众号哪天被封搞个鸡儿。
什么平台都不要过渡依赖。那年渣浪被整改,清空了所有用户网盘存的东西,我多年在渣浪积累的无形资产文档都没了。
网友存档
https://info.williamlong.info/2016/04/blog-post_25.html
不要徒方便把自己的无形资产交给平台,搞公众号不如学哇酷站长搞个网站,牢牢拽在自己手里的无形资产。
离线
微信公众号个性化菜单: https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Personalized_menu_interface.html#0
匹配条件, 分组/性别/地域/操作系统类型/语言 等:
"matchrule": {
"tag_id": "2",
"sex": "1",
"country": "中国",
"province": "广东",
"city": "广州",
"client_platform_type": "2",
"language": "zh_CN"
}
执行文件 addconditional_menu.php 注册菜单, 返回菜单编号Id:
<?php
//通过微信获取access_token接口 获取的token
$ACCESS_TOKEN = '36_wH1VlEPwtqU5hhiS9QNayaoBON4h4MNH1ICWNAK8A5pBQpQ5Jx0-k1dAvT4ixloPaCgBFtlc15D-OAvxfraTIizvPzY-A96ubwtPpLWxTjKyL605hdKFt3unigoXt-AwKWDY5Q9fz53Rwl5CKQWfAGAEK';
$openid = 'oVEjzvmgLIF0oLHYccqGZnYyZ4r4';//用户openid
$template_id = 'dGjqe_2azli_K2W_T0Niel2823LxuoWS-VzB3Lp5goU';//配置的模板id
$url = '[url]https://xxx.cn/[/url]';//点击模板消息跳转的链接
//个性化菜单字符串
$menujson = '{
"button":[
{
"type":"click",
"name":"单击",
"key":"V1001_TODAY_MUSIC"
},
{
"name":"搜索",
"sub_button":[
{
"type":"view",
"name":"百度",
"url":"http://www.baidu.com/"
}
]
}],
"matchrule":{
"sex":"1"
}
}';
$send_template_url = '[url]https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=[/url]' . $ACCESS_TOKEN;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $send_template_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $menujson);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$res = curl_exec($ch);
//$rinfo = curl_getinfo($ch);
//print_r($rinfo);
//输出收到的 json 回应: {"menuid":425482849}
printf("\n\n%s\n\n ---------------- \n\n", $res);
curl_close($ch);
return $res;
?>
关注的用户(openid)应用到该注册菜单 trymatch_menu.php:
<?php
//通过微信获取access_token接口 获取的token
$ACCESS_TOKEN = '36_wH1VlEPwtqU5hhiS9QNayaoBON4h4MNH1ICWNAK8A5pBQpQ5Jx0-k1dAvT4ixloPaCgBFtlc15D-OAvxfraTIizvPzY-A96ubwtPpLWxTjKyL605hdKFt3unigoXt-AwKWDY5Q9fz53Rwl5CKQWfAGAEK';
$openid = 'oVEjzvmgLIF0oLHYccqGZnYyZ4r4';//用户openid
$template_id = 'dGjqe_2azli_K2W_T0Niel2823LxuoWS-VzB3Lp5goU';//配置的模板id
$url = 'https://xxx.cn/';//点击模板消息跳转的链接
$json_trymenu = sprintf('{"user_id": "%s"}', $openid);
$send_template_url = 'https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token=' . $ACCESS_TOKEN;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $send_template_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_trymenu);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$res = curl_exec($ch);
printf("\n\n%s\n\n ---------------- \n\n", $res);
curl_close($ch);
return $res;
?>
收到字符串:
{"menu":{"button":[{"type":"click","name":"单击","key":"V1001_TODAY_MUSIC","sub_button":[]},{"name":"搜索","sub_button":[{"type":"view","name":"百度", "url":"http:\/\/www.baidu.com\/","sub_button":[]}]}]}}
然后你打开微信, 发现菜单是不是变了.
如果还没有变, 先取消关注, 再加关注即可。
离线
水煮鱼是个框,什么都能往里装!
离线
搞得连微信都不能正常使用了?
离线
流明 抽时间学学
离线