事例代码也跑不通。我研究了一番,终于搞定。
首先:
在你的服务器上上传好一个接口文件,如http://www.xxx.com/weixin.php 内容如下:
代码如下 复制代码
?valid();
//$wechatobj->responsemsg();
class wechatcallbackapitest
{
public function valid()
{
$echostr = $_get[echostr];
if($this->checksignature()){
echo $echostr;
exit;
}
}
public function responsemsg()
{
$poststr = $globals[http_raw_post_data];
if (!empty($poststr)){
$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
$fromusername = $postobj->fromusername;
$tousername = $postobj->tousername;
$keyword = trim($postobj->content);
$time = time();
$texttpl =
%s
0
;
if(!empty( $keyword ))
{
$msgtype = text;
$contentstr = mess;
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
echo $resultstr;
}else{
echo mess;
}
}else {
echo mess;
exit;
}
}
private function checksignature()
{
$signature = $_get[signature];
$timestamp = $_get[timestamp];
$nonce = $_get[nonce];
$token =token;
$tmparr = array($token, $timestamp, $nonce);
sort($tmparr);
$tmpstr = implode( $tmparr );
$tmpstr = sha1( $tmpstr );
if( $tmpstr == $signature ){
return true;
}else{
return false;
}
}
}
?>
然后:
设置回复接口,填好url和token(必须跟上面定义的token一致)
这个接口先要验证,成功之后
再注释掉$wechatobj->valid(); 这行,同时去掉//$wechatobj->responsemsg();这行的注释。
消息接口就可以使用了,发个消息试试看吧?
