2014-09-05
要成为微信公众号(订阅号或服务号)的开发者,需要首先验证接口,这个可以在登录微信https://mp.weixin.qq.com后台后设置。但是我嫌麻烦,于是开发个接口类,包含验证函数(还有回复文本信息和图文信息的功能)。其实接口验证在成为开发者之后就没用了。
上代码,微信基类:weixin.class.php
class Weixin
{
public $token = '';//token
public $debug = false;//是否debug的状态标示,方便我们在调试的时候记录一些中间数据
public $setFlag = false;
public $msgtype = 'text'; //('text','image','location')
public $msg = array();
public function __construct($token,$debug)
{
$this->token = $token;
$this->debug = $debug;
}
//获得用户发过来的消息(消息内容和消息类型 )
public function getMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if ($this->debug)
{
$this->write_log($postStr);
}
if (!empty($postStr))
{
$this->msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->msgtype = strtolower($this->msg['MsgType']);
}
}
//回复文本消息
public function makeText($text='')
{
$CreateTime = time();
$FuncFlag = $this->setFlag ? 1 : 0;
$textTpl = "
return sprintf($textTpl,$text,$FuncFlag);
}
//根据数组参数回复图文消息
public function makeNews($newsData=array())
{
$CreateTime = time();
$FuncFlag = $this->setFlag ? 1 : 0;
$newTplHeader = "
%s";
$newTplItem = "
$newTplFoot = "
$Content = '';
$itemsCount = count($newsData);
$itemsCount = $itemsCount if ($itemsCount)
{
foreach ($newsData as $key => $item)
{
if ($key {
$Content .= sprintf($newTplItem,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']);
}
}
}
$header = sprintf($newTplHeader,$newsData['content'],$itemsCount);
$footer = sprintf($newTplFoot,$FuncFlag);
return $header . $Content . $footer;
}
public function reply($data)
{
if ($this->debug)
{
$this->write_log($data);
}
echo $data;
}
public function valid()
{
if ($this->checkSignature())
{
//if( $_SERVER['REQUEST_METHOD']=='GET' )
//{
echo $_GET['echostr'];
exit;
//}
}
else
{
write_log('认证失败');
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$tmpArr = array($this->token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature )
return true;
else
return false;
}
private function write_log($log)
{
//这里是你记录调试信息的地方 请自行完善 以便中间调试
}
}
?>
微信接口的代码:weixin.php
header("Content-Type: text/html;charset=utf-8");
include_once('weixin.class.php'); //引用刚定义的微信消息处理类
define("TOKEN", "itwatch"); //mmhelper
define('DEBUG', false);
$weixin = new Weixin(TOKEN, DEBUG); //实例化
//$weixin->valid();
$weixin->getMsg();
$type = $weixin->msgtype; //消息类型
$username = $weixin->msg['FromUserName']; //哪个用户给你发的消息,这个$username是微信加密之后的,但是每个用户都是一一对应的
if ($type==='text')
{
//if ($weixin->msg['Content']=='Hello2BizUser')
if ($weixin->msg['Content']=='你好')
{ //微信用户第一次关注你的账号的时候,你的公众账号就会受到一条内容为'Hello2BizUser'的消息
$reply = $weixin->makeText('欢迎你关注网眼视界威信公众平台');
}
else
{ //这里就是用户输入了文本信息
$keyword = $weixin->msg['Content']; //用户的文本消息内容
//include_once("chaxun.php"); //文本消息 调用查询程序
//$chaxun= new chaxun(DEBUG, $keyword, $username);
//$results['items'] =$chaxun->search(); //查询的代码
//$reply = $weixin->makeNews($results);
$arrayCon = array(
array(
"Title"=>"电脑学习网",
"Description"=>"十万个为什么-电脑学习网",
"PicUrl"=>"http://www.veryphp.cn/datas/userfiles/8bd108c8a01a892d129c52484ef97a0d/images/website13.jpg",
"Url"=>"http://www.why100000.com/"
),
array(
"Title"=>"非常PHP学习网",
"Description"=>"大型PHP学习分享社区",
"PicUrl"=>"http://www.veryphp.cn/datas/userfiles/8bd108c8a01a892d129c52484ef97a0d/images/php01.jpg",
"Url"=>"http://www.veryphp.cn/"
)
);
$results = $arrayCon;
$reply = $weixin->makeNews($results);
}
}
elseif ($type==='location')
{
//用户发送的是位置信息 稍后处理
}
elseif ($type==='image')
{
//用户发送的是图片 稍后处理
}elseif ($type==='voice')
{
//用户发送的是声音 稍后处理
}
//
$weixin->reply($reply);
?>
验证微信接口的代码,用 curl 函数完成,需要打开PHP的 curl 扩展。把 weixin.php 文件中的 //$weixin->valid(); 一句的注释去掉即可验证,完了把这句注释掉即可。
function signature($token, $timestamp, $nonce)
{
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
return $tmpStr;
}
//提交
$post_data = array(
"signature=$signature",
"timestamp=$timestamp",
"nonce=$nonce",
"echostr=$echostr"
);
$post_data = implode('&',$post_data);
$url='http://www.veryphp.cn/tools/weixin/weixin.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.'?'.$post_data); //模拟GET方法
ob_start();
curl_exec($ch);
$result = ob_get_contents();
ob_end_clean();
echo $result;
?>
以上的核心代码是 weixin.class.php 和 weixin.php 两个文件,是我调试成功的,已经部署在我的服务器上了。你要测试的话,用手机微信收听微信号:itwatch,然后输入“你好”,会返回字符串:欢迎你关注网眼视界威信公众平台。随便输入,会打开一个图文消息。
好吧,我承认以上代码写的非常凌乱,因为我十分瞌睡了, 要睡觉了。但以上代码确实是能工作的,是典型的原理实现性测试代码。希望给微信开发者提供个思路,看明白之后可以结合数据库写一个功能完善的微信信息后台管理程序。。
有微信服务号的,可以在此基础上开发个菜单,然后调用仿照以上代码开发的消息回复系统。其实很简单。
这才是真正的网络通信程序,比你写企业站,把数据输进去,再按顺序检索出来分页显示,要有意思的多。
网眼-张庆
2013-12-3 ?
1
CI框架连接数据库配置操作以及多数据库操作
09-05
2
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
05-17
3
C语言关键字及其解释介绍 C语言32个关键字详解
04-05
4
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
04-26
5
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
04-12
6
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
09-05
7
PHP中的(++i)前缀自增 和 (i++)后缀自增
09-05
8
PHP中include和require区别之我见
09-05
常用dos命令及语法
2014-09-27
将视频设置为Android手机开机动画的教程
2014-12-11
php递归返回值的问题
2014-09-05
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
2017-05-03
java中的info是什么意思
2022-03-24
PHP 教程之如何使用BLOB存取图片信息实例
2014-09-05
IcePHP框架中的快速后台中的通用CRUD功能框架
2014-09-05
单片机编程好学吗?单片机初学者怎样看懂代码
2022-03-21
PHP数组函数array
2014-09-05
学ug编程如何快速入门?
2022-03-17
拳皇14官方正版下载v2.0.0 安卓正式版
动作闯关 1.06G
下载勇者大战魔物娘安卓手游下载v1.10.29 安卓冷狐汉化版
角色扮演 792.3M
下载贝比岛最新版下载v2.5.4 安卓官方版
其它手游 32.9M
下载奥特曼格斗进化3高清汉化版(Ultraman Fighting Evolution 3)下载v3.3.2 安卓免费版
动作闯关 2.19G
下载王者荣耀全英雄全皮肤版本下载v10.11.7.1 安卓版
其它手游 454.5M
下载漫威超级战争手游下载v3.23.0 安卓手机版
动作闯关 1.90G
下载悟饭游戏厅苹果版(我Fun趣味)下载v1.5.6 iPhone版
其它手游 125.7M
下载无畏契约valorant官方版下载v1.0.3 安卓版
射击枪战 150.3M
下载飞羽青春羽毛球游戏下载v1.9.2 安卓官方版
下载
斗罗大陆诛邪传说手游下载v2.0.18 安卓版
下载
kisakibluearchive(碧蓝档案)下载v1.0 安卓手机版碧蓝档案妃咲同人小游戏
下载
恐怖奶奶联机版游戏下载v1.4.1.5 安卓手机版
下载
失落之城中文版下载v8.8 官方安卓版
下载
舰载机着舰模拟器最新版本(Carrier Landing HD)下载v2020.6.02 安卓版
下载
边境之旅最新版本下载v4.2.0 安卓完整版
下载
植物大战僵尸PVZ指导版2.0下载v1.0 安卓版
下载