微信公众平台服务类,可实现验证token,回复消息,回复图文,自定义图文,自定义跳转等基本功能,前提是要知道在哪里改。分享出来了。
把server.php和wechat.phpt放在同一目录(www)下,会改路径的当我没说。验证时填服务器地址http........./server.php.
token可以根据代码自己改。
废话不多说,上源码。
Wechat.php
<?php
class Wechat {
private $debug;
private $request;
public function __construct($token, $debug = FALSE) {
if ($this->isValid() && $this->validateSignature($token)) {
exit($_GET['echostr']);
}
$this->debug = $debug;
set_error_handler(array(&$this, 'errorHandler'));
$xml = (array) simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'], 'SimpleXMLElement', LIBXML_NOCDATA);
$this->request = array_change_key_case($xml, CASE_LOWER);
}
private function isValid() {
return isset($_GET['echostr']);
}
private function validateSignature($token) {
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$signatureArray = array($token, $timestamp, $nonce);
sort($signatureArray);
return sha1(implode($signatureArray)) == $signature;
}
protected function getRequest($param = FALSE) {
if ($param === FALSE) {
return $this->request;
}
$param = strtolower($param);
if (isset($this->request[$param])) {
return $this->request[$param];
}
return NULL;
}
protected function onSubscribe() {}
protected function onUnsubscribe() {}
protected function onText() {}
protected function onImage() {}
protected function onLocation() {}
protected function onLink() {}
protected function onUnknown() {}
protected function responseText($content, $funcFlag = 0) {
exit(new TextResponse($this->getRequest('fromusername'), $this->getRequest('tousername'), $content, $funcFlag));
}
protected function responseMusic($title, $description, $musicUrl, $hqMusicUrl, $funcFlag = 0) {
exit(new MusicResponse($this->getRequest('fromusername'), $this->getRequest('tousername'), $title, $description, $musicUrl, $hqMusicUrl, $funcFlag));
}
protected function responseNews($items, $funcFlag = 0) {
exit(new NewsResponse($this->getRequest('fromusername'), $this->getRequest('tousername'), $items, $funcFlag));
}
public function run() {
switch ($this->getRequest('msgtype')) {
case 'event':
switch ($this->getRequest('event')) {
case 'subscribe':
$this->onSubscribe();
break;
case 'unsubscribe':
$this->onUnsubscribe();
break;
}
break;
case 'text':
$this->onText();
break;
case 'image':
$this->onImage();
break;
case 'location':
$this->onLocation();
break;
case 'link':
$this->onLink();
break;
default:
$this->onUnknown();
break;
}
}
protected function errorHandler($level, $msg, $file, $line) {
if ( ! $this->debug) {
return;
}
$error_type = array(
// E_ERROR => 'Error',
E_WARNING => 'Warning',
// E_PARSE => 'Parse Error',
E_NOTICE => 'Notice',
// E_CORE_ERROR => 'Core Error',
// E_CORE_WARNING => 'Core Warning',
// E_COMPILE_ERROR => 'Compile Error',
// E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Strict',
E_RECOVERABLE_ERROR => 'Recoverable Error',
E_DEPRECATED => 'Deprecated',
E_USER_DEPRECATED => 'User Deprecated',
);
$template = <<<ERR
PHP 报错
%s: %s
File: %s
Line: %s
ERR;
$this->responseText(sprintf($template,
$error_type[$level],
$msg,
$file,
$line
));
}
}
abstract class WechatResponse {
protected $toUserName;
protected $fromUserName;
protected $funcFlag;
public function __construct($toUserName, $fromUserName, $funcFlag) {
$this->toUserName = $toUserName;
$this->fromUserName = $fromUserName;
$this->funcFlag = $funcFlag;
}
abstract public function __toString();
}
class TextResponse extends WechatResponse {
protected $content;
protected $template = <<<XML
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%s<FuncFlag>
</xml>
XML;
public function __construct($toUserName, $fromUserName, $content, $funcFlag = 0) {
parent::__construct($toUserName, $fromUserName, $funcFlag);
$this->content = $content;
}
public function __toString() {
return sprintf($this->template,
$this->toUserName,
$this->fromUserName,
time(),
$this->content,
$this->funcFlag
);
}
}
class MusicResponse extends WechatResponse {
protected $title;
protected $description;
protected $musicUrl;
protected $hqMusicUrl;
protected $template = <<<XML
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>
<FuncFlag>%s<FuncFlag>
</xml>
XML;
public function __construct($toUserName, $fromUserName, $title, $description, $musicUrl, $hqMusicUrl, $funcFlag) {
parent::__construct($toUserName, $fromUserName, $funcFlag);
$this->title = $title;
$this->description = $description;
$this->musicUrl = $musicUrl;
$this->hqMusicUrl = $hqMusicUrl;
}
public function __toString() {
return sprintf($this->template,
$this->toUserName,
$this->fromUserName,
time(),
$this->title,
$this->description,
$this->musicUrl,
$this->hqMusicUrl,
$this->funcFlag
);
}
}
class NewsResponse extends WechatResponse {
protected $items = array();
protected $template = <<<XML
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>
%s
</Articles>
<FuncFlag>%s<FuncFlag>
</xml>'
XML;
public function __construct($toUserName, $fromUserName, $items, $funcFlag) {
parent::__construct($toUserName, $fromUserName, $funcFlag);
$this->items = $items;
}
public function __toString() {
return sprintf($this->template,
$this->toUserName,
$this->fromUserName,
time(),
count($this->items),
implode($this->items),
$this->funcFlag
);
}
}
class NewsResponseItem {
protected $title;
protected $description;
protected $picUrl;
protected $url;
protected $template = <<<XML
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
XML;
public function __construct($title, $description, $picUrl, $url) {
$this->title = $title;
$this->description = $description;
$this->picUrl = $picUrl;
$this->url = $url;
}
public function __toString() {
return sprintf($this->template,
$this->title,
$this->description,
$this->picUrl,
$this->url
);
}
}
Server.php
<?php
require('Wechat.php');
class MyWechat extends Wechat {
protected function onSubscribe() {
$this->responseText('欢迎关注');
}
protected function onUnsubscribe() {
}
protected function onText() {
$this->responseText('收到了文字消息:' . $this->getRequest('content'));
}
protected function onImage() {
$items = array(
new NewsResponseItem('标题一', '描述一', $this->getRequest('picurl'), $this->getRequest('picurl')),
new NewsResponseItem('标题二', '描述二', $this->getRequest('picurl'), $this->getRequest('picurl')),
);
$this->responseNews($items);
}
protected function onLocation() {
$num = 1 / 0;
$this->responseText('收到了位置消息:' . $this->getRequest('location_x') . ',' . $this->getRequest('location_y'));
}
protected function onLink() {
$this->responseText('收到了链接:' . $this->getRequest('url'));
}
protected function onUnknown() {
$this->responseText('收到了未知类型消息:' . $this->getRequest('msgtype'));
}
}
$wechat = new MyWechat('weixin', TRUE);
$wechat->run();