1、首先我们要有域名
2、在服务器上创建一个文件wechatCallbackapiTest.php
3、文件写入以下代码
<?php
/**
* wechat php test
*/
//默认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 = file_get_contents('php://input');
//extract post data
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;