1、首先需要一个可以外网访问的接口url。
我这里是申请的新浪免费云服务器,http://xxxxx.applinzi.com/wx.php,具体自己可以去新浪云中心申请地址为:http://www.sinacloud.com
2、去微信公众账号申请个人的公众账号,地址为:https://mp.weixin.qq.com,然后进入到左侧菜单开发里面选择基本配置。
3、点击修改配置,填写必要的信息,认证如下图:
需要在wx.php里面写入认证代码,具体代码下文会提供。
4、等待认证成功后,启用配置
附详细代码:
<?php
header('Content-type:text/html;charset=utf-8');
define("TOKEN", "这里是你在微信那里的token");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid(); class wechatCallbackapiTest {
public function valid()
{
//这里是认证过程
$echoStr = $_GET["echostr"];
if($this->checkSignature() && $echoStr) {
echo $echoStr;
exit;
} else {
$this->responseMsg();//当认证成功后执行下面的代码
}
} private function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)) {
libxml_disable_entity_loader(true);
$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>";
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
//第一次关注的时候
if (strtolower($postObj->MsgType) == 'event') {
if (strtolower($postObj->Event) == 'subscribe') {
$toUsername = $postObj->ToUserName;
$fromUsername = $postObj->FromUserName;
$time = time();
$msgType = "text";
$contentStr = "<<Welcome attention to life is elsewhere!>>";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
//关键字文本回复
} else if ( strtolower($postObj->MsgType) == 'text' ) {
if ( $postObj->Content == 'imooc' ) {
$toUsername = $postObj->ToUserName;
$fromUsername = $postObj->FromUserName;
$time = time();
$msgType = "text";
$contentStr = "imooc is very good";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
} else if ( $postObj->Content == '美丽说' ) {
$array = array(
array(
'title' => 'meilishuo',
'description'=> 'meilishuo is very good',
'picurl' => 'http://d05.res.meilishuo.net/img/_o/67/24/65bc4ebfe22d0c2eca1702c9736c_117_43.ch.png',
'url' => 'http://www.meilishuo.com'
)
);
$imageTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>".count($array)."</ArticleCount>
<Articles>";
foreach ($array as $key => $value) {
$imageTpl .=
"<item>
<Title><![CDATA[".$value['title']."]]></Title>
<Description><![CDATA[".$value['description']."]]></Description>
<PicUrl><![CDATA[".$value['picurl']."]]></PicUrl>
<Url><![CDATA[".$value['url']."]]></Url>
</item>";
}
$imageTpl .= "</Articles></xml>";
$toUsername = $postObj->ToUserName;
$fromUsername = $postObj->FromUserName;
$time = time();
$msgType = "news";
$resultStr = sprintf($imageTpl, $fromUsername, $toUsername, $time, $msgType);
echo $resultStr;
exit;
}
} } else {
echo "";
exit;
} } private function checkSignature() {
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
} $signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
return ($tmpStr == $signature) ? true : false;
}
}