微信公众号开发1搭建php服务器

来源:互联网 发布:迷失岛结局含义知乎 编辑:程序博客网 时间:2024/05/29 20:03

1 本来就买了阿里云的服务器,就直接使用了,新浪云SAE总是访问不到,如果没有服务器的也可以参考去使用免费的新浪云或者百度云;这里仅仅提供链接

方倍工作室,讲得是很好的   http://www.cnblogs.com/txw1958/

2 这里我直接使用阿里云ECS开发 , 这里使用Oneinstack自动部署开发环境, 有需要的可以访问这里  https://market.aliyun.com/products/55528001/jxsc000238.html?spm=5176.730005.0.0.rQDua5

使用比较方便, 这里我使用了域名配置 http://weixing.lazhuwang.com.cn/ 指向我的阿里云服务器;下载wx_simple.php改名为index.php就可以,然后修改一下

把原本的$wechatObj->valid(); 改成 $wechatObj->responseMsg(); 也就是返回消息
 




然后再修改responseMsg() 方法中的  $contentStr="欢迎访问小熊之家"; 这里是修改返回消息, 可以随意修改.然后上传到服务器的根目录下面即可

上传完成后配置公众号, 打开 登陆微信公众号后打开 开发->基本配置 里面的



类似这样的配置自己的域名, Token 默认是  weixin EncodingAESKey(消息加解密密钥)自动生成即可

然后就可以打开公众号给他发消息了



这里就完成了简单的自动回复功能,如果没有wx_simple.php ,这里给出吧,有时候不好找这个



/**

* wechat php test

*/

//define your token

define("TOKEN","weixin");

$wechatObj=newwechatCallbackapiTest();

$wechatObj->valid();

classwechatCallbackapiTest

{

public functionvalid()

{

$echoStr=$_GET["echostr"];

//valid signature , option

if($this->checkSignature()){

echo$echoStr;

exit;

}

}

public functionresponseMsg()

{

//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();

$textTpl="

%s

0

";

if(!empty($keyword))

{

$msgType="text";

$contentStr="Welcome to wechat world!";

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);

echo$resultStr;

}else{

echo"Input something...";

}

}else{

echo"";

exit;

}

}

private functioncheckSignature()

{

$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;

}

}

}

?>

0 0
原创粉丝点击