顺晟科技
2021-06-16 10:56:33
248
前段时间弄dedecms,微信公众号那么火,就想集成下,百度搜了下教程很多,大体都差不多,结果用了后不行,在公众号里回复关键词后,乱码,乱码,乱码,更有的教程代码贴上之后直接不回复,无论你怎么发就是不回复你,很是蛋疼,下面中狐博主把18年测试成功的代码贴上来,至于怎么注册微信公众号等就不发了,不会的自行百度。将代码保存为weixin.php放加下就好
?服务器端编程语言(Professional Hypertext Preprocessor的缩写)定义(' TOKEN ','你的代币名字');require_once(dirname(__FILE__).'/./include/common。Inc . PHP ');$ dsql=new DEDESKL(false);$微信=新微信回调();$微信object-valid();
班级微信回拨
{
私人$ items=
private $ article COunt=0;
私人$关键字=' ';
公共函数有效()
{
$ echoStr=$ _ GET[' echoStr '];
//有效签名,选项
if($this-checkSignature()){
echo $ echoStr
$ this-response msg();
退出;
}
}
公共功能响应SgG()
{
//获取帖子数据,可能是由于环境不同
$ PostStr=$ GLOBALS[' HTTP _ RAW _ POST _ DATA '];
//提取帖子数据
if(!空($postStr)){
$ PostObj=simple XML _ load _ string($ PostStr,' SimpleXMLElement ',libXML _ NocDATa);
$ FromUserName=$ PostObj-FromUserName;
$ TouseName=$ PostObj-TouseName;
$ this-keyword=trim($ PostObj-Content);
$ time=time();
$textTpl='
![CData[1 .$fromUsername . ']]
![CData[1 .$ toUsername . ']]。$时间.
![CData[文本]]
![CDATA[%s]]
0
;
$picTpl='
![CData[1 .$fromUsername . ']]
![CData[1 .$ toUsername . ']]。$时间.
![CData[新闻]]
%d
%s
一
;
if($ this-keyword==' hi ' | | $ this-keyword=='您好| | $这个-关键字=='你好| | $这个-关键字==
hello 2 biz用户"){
$contentStr='输入关键字开始搜索!';//自定义欢迎回复;
echo sprintf($textTpl,$ Contentstr);
}else if(!empty($this-keyword )){
$ this-search();
if($this-articleCount==0){
$contentStr='抱歉,没有找到与【{$this-keyword}】相关的文章,要不你更换一下关键字,可能就有结果了哦:-) ';
echo sprintf($textTpl,$ Contentstr);
}else{
echo sprintf($picTpl,$this-articleCount,$ this-items);
}
}
}else {
回声"";
退出;
}
}
私有函数搜索(){
global $ dsql
$ weixin _ post=$ dsql-Setquery(' Select * From ` dede _ archives `其中标题如"%"。$这个-关键字。%'
按id desc限制订购0,5 ');
$ items=
$ dsql-Execute();
while($ weixin _ post=$ dsql-GetObject()){
$ title=$ weixin _ post-title;
$摘录=$ weixin _ post-description;//获取摘要
$ thumb=$ weixin _ post-litpic;//获取缩略图;
$link='/m/view.php?aid=' .$ weixin _ post-id;
$items=$items .$this-get_item($title,$ extract,$thumb,$ link);
$ this-article COunt;
}
if($ this-article COunt 5)$ this-article COunt=5;
$ this-items=$ items;
}
私有函数get_item($title,$description,$picUrl,$url){
if(!$ description)$ description=$ title;
返回
'
![CData[1 .$描述.]]
![CDATA[http://' .$_SERVER['HTTP_HOST'].$picUrl ']]
![CDATA[http://' .$_SERVER['HTTP_HOST'].$url .]]
;
}
私有函数checkSignature()
{
$ signature=$ _ GET[' signature '];
$ timestamp=$ _ GET[' timestamp '];
$ nonce=$ _ GET[' nonce '];
$ token=TOKEN
$tmpArr=array($token,$timestamp,$ nonce);
sort($tmpArr,SORT _ STRING);
$tmpStr=内爆($ TMParr);
$ TMPstr=sha1($ TMPstr);
if($tmpStr==$signature ){
返回真实的
}else{
返回错误的
}
}
}
重要点
$ this-keyword=trim($ PostObj-Content);这段代码解决的乱码问题。我的是utf-8版本的。
重要第二点
sort($tmpArr,SORT _ STRING);
这段代码是解决的有时收到信息有时收不到信息,这个在14 年的时候微信升级,对排序算法进行了修改,原有的排序($tmpArr)修改为sort($tmpArr,SORT_STRING),网上教程都说14 年之前没升级的教程,所以很多朋友用了后有时收到信息有时收不到信息。
重要第三点
$link='/m/view.php?aid=' .$ weixin _ post-id;
在微信上打开的话,一定要打开手机版本,所以如果你的手机版本在目录下的M文件夹里,就要按照上面的填写。如果跳转到手机版,可以根据不同情况进行修改。
04
2022-06
17
2022-03
16
2021-06
16
2021-06
16
2021-06
16
2021-06