这个扩展实现了sp和微信公众平台的对接,1.0版暂时只实现了最简单的功能:绑定,收信息,回复信息。
扩展配置方法:
$spconfig = array(
'mode' => 'debug'
//扩展配置
, 'ext' => array(
//微信扩展设置
'spweixin' => array(
'token' => 'weixin' //微信通信密钥,后台设置
)
)
); 然后在controll的方法里面执行:
$wx = spclass('spweixin');
$msg = $wx->run(); 就行了,如果是绑定那么会直接输出微信指定显示的echostr,程序中止执行。如果是收到信息,那么$msg就是收到的信息的数组,键名对照微信官方文档就成。
token = $params['token'];
}
/**
* 绑定微信接口
* @return stringfalse
*/
public function bind() {
//随机字符串
$echostr = $_get[echostr];
//微信加密签名
$signature = $_get[signature];
//签名时间戳
$timestamp = $_get[timestamp];
//加密随机数
$nonce = $_get[nonce];
/*
* 加密/校验流程:
1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
*/
$tmparr = array($this->token, $timestamp, $nonce);
sort($tmparr);
$_signature = sha1(implode($tmparr));
if ($_signature != $signature) {
//签名不正确,返回false
$echostr = false;
}
return $echostr;
}
/**
* 接收消息,返回消息数组
* @return array
*/
public function receive() {
$raw = $this->php_fix_raw_query();
$msg = false;
if (!empty($raw)) {
$msg = (array) simplexml_load_string($globals['http_raw_post_data'], 'simplexmlelement', libxml_nocdata);
$this->publicuserid = $msg['tousername'];
$this->touserid = $msg['fromusername'];
}
return $msg;
}
/**
* 获取 post 提交的原始数据
* @author robotreply at gmail dot com (24-jul-2009 08:17)
* @return string
*/
private function php_fix_raw_query() {
// try globals array
//试图从全局变量中获取
if (isset($globals[http_raw_post_data])) {
return $globals[http_raw_post_data];
}
// try globals variable
//试图从全局变量中获取
if (isset($http_raw_post_data)) {
return $http_raw_post_data;
}
$post = '';
// try stream
//试图从流中获取
if (!function_exists('file_get_contents')) {
//php://input is not available with enctype=multipart/form-data.
$fp = fopen(php://input, r);
if ($fp) {
while (!feof($fp)) {
$post = fread($fp, 1024);
}
fclose($fp);
}
} else {
$post = file_get_contents(php://input);
}
return $post;
}
/**
* 回复文本消息
*
对于每一个post请求,开发者在响应包中返回特定xml结构
*
对该消息进行响应(现支持回复文本、图文、语音、视频、音乐和对收到的消息进行星标操作)
*
微信服务器在五秒内收不到响应会断掉连接。
* @param string $content 回复的消息内容
* @return stringfalse
*/
public function replytext($content) {
$msg = false;
if (!empty($content)) {
//createtime 消息创建时间
$createtime = time();
$msg =
touserid}]]>
publicuserid}]]>
{$createtime}
xml;
}
return $msg;
}
/**
* 回复音乐消息
* @param string $title 标题
* @param string $description 描述
* @param string $musicurl 音乐链接
* @param string $hqmusicurl 高质量音乐链接,wifi环境优先使用该链接播放音乐
* @return stringfalse
*/
public function replymusic($title, $description, $musicurl, $hqmusicurl) {
$msg = false;
if (!empty($musicurl)) {
//createtime 消息创建时间
$createtime = time();
$msg =
touserid}]]>
publicuserid}]]>
{$createtime}
xml;
}
return $msg;
}
/**
* 回复图文消息
* @param type $articles 文章列表 array(array(title,picurl,url))
* @return stringfalse
*/
public function replynews($articles) {
$msg = false;
$articlesstr = '';
//图文消息个数,限制为10条以内
$articlecount = 0;
foreach ($articles as $_art) {
if (!empty($_art['title']) && !empty($_art['picurl']) && !empty($_art['url'])) {
$articlecount++;
$articlesstr .=
;
}
}
if (!empty($articlecount)) {
//createtime 消息创建时间
$createtime = time();
$msg =
touserid}]]>
publicuserid}]]>
{$createtime}
{$articlecount}
{$articlesstr}
xml;
}
return $msg;
}
/**
* 运行
* @return type
*/
function run() {
//微信服务器每次请求都会将signature,timestamp,nonce三个参数get到接口
//只能通过是否存在echostr来判断是否是接口绑定动作
if (isset($_get['echostr'])) {
//绑定
exit($this->bind());
} else {
//收到信息
return $this->receive();
}
}
}
?>