其实就是认证,然后发送消息。将文件传到你的服务器上,我放在根目录下,然后修改开发模式下的url和token值。假设这里使用的url是//www.jb51.net/wx_sample.php,token就是上面define的token,这个可以改的,只要两边保持一致,默认是weixin。然后点提交,就会提示你成功了。然后扫下你申请的号码,发个消息,你会发现没反应,这个时候我们需要小调整一下,关闭接口文档中调用认证的方法,开启调用处理回复信息的方法:
//$wechatObj->valid(); $wechatObj->responseMsg();
这个时候你再发个消息,你就会收到:Welcome to wechat world!
是不是在关注了有些订阅号或者服务号之后,马上会收到一条消息。什么回复1,怎样怎样;回复2,怎样怎样之类的。
拿我自己的博客举例,我的关注语是:
感谢您关注AndyYang个人博客微信小助手。
回复【1】返回两篇最新文章
回复【2】返回两篇人气文章
回复【3】返回两篇热评文章
回复【4】返回两篇最新技术文章
回复【5】返回两篇最新写作文章
回复其他返回搜索关键字的两篇文章
更多精彩内容,尽在:www.jb51.net。亲们,请多多支持哦,谢谢~
那这个怎么实现呢?直接上代码:
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract 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();
$MsgType = $postObj->MsgType; //add
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if($MsgType != 'event') {
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "Welcome to wechat world!";
}else{
echo "Input something...";
}
} else {
$msgType = "text";
$contentStr = "感谢您关注AndyYang个人博客微信小助手。rn".
"回复【1】返回两篇最新文章rn".
"回复【2】返回两篇人气文章rn".
"回复【3】返回两篇热评文章rn".
"回复【4】返回两篇最新技术文章rn".
"回复【5】返回两篇最新写作文章rn".
"回复其他返回搜索关键字的两篇文章rn".
"更多精彩内容,尽在:<a href='//www.jb51.net'>www.jb51.net</a>。亲们,请多多支持哦,谢谢~";
;
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else {
echo "";
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING); //这个在新的sdk中添加了第二个参数(compare items as strings)
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}







