微信公众平台的应用开发

widuu 2013-3-1 PHP技术 4,517 0 喜欢 (0)

微信公众平台是个可以开发成不错的应用,譬如php100的php函数查询,小贱鸡等都是基于微信公众平台的,今天我就对于微信开发平台给大家详细的介绍一下!

首先先申请微信公众平台地址:http://mp.weixin.qq.com

2.设置好微信公众平台账号,点击高级功能关闭编辑模式,然后打开开发者模式

3.点击成为开发者,然后提交你的url记住是你的文件名到譬如http://www.widuu.com/weixin.class.php!然后随意填写你的token,不要点击提交!下载下边的文件,修改token对应你自己编写的token!
[php]
<?php
/**
* wechat php test
*/

//define your token
define("TOKEN", "xiaowei");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
//等你提交完成后,注释掉上边的$wechatObj->valid();然后去掉//$wechatObj->responseMsg();的// 然后就可以了
//$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();
$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(!empty( $keyword ))
{
//结合数据库然后开发你的逻辑
switch($keyword){
case "php":
$msgType = "text";
$content="欢迎大家学习PHP";
$result=sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $content);
echo $result;
break;
case "肖伟" :
$msgType = "text";
$content="博客作者呀???";
$result=sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $content);
echo $result;
break;
default:
$msgType = "text";
$contentStr = "刚刚开始还没有您需要的数据";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
break;

}

}else{
echo "Input something...";
}

}else {
echo "";
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;
}
}
}

?>
[/php]

我的公众平台

转载请注明来自微度网络,本文标题:《微信公众平台的应用开发》

喜欢 0 发布评论
Top