微信公众账号开发服务接入指南

Home / Article MrLee 2015-3-2 3867

微信公众账号平台已经非常火爆了,入驻的用户群已经非常庞大了。今天也非常荣幸成为其中一员,因为本身是开发人员,所以开通了开发中心接口,现在把服务配置纪录下来,方便后面加入的朋友学习。对了,这个仅对有自己服务器的用户有效,或者你有免费的服务器也OK!
下面说说操作,成功登录公众账号之后左边菜单最下面有一个开发者中心,如图

20150302225647

点击进入,然后点击右边的修改配置,这里要注意的是URL地址为你开放的PHP或者其它WEB服务地址,如http://www.it72.com/api.php,然后TOKEN随便填就行了,主要是为了验证,这里我设置为weixin,验证代码用PHP实现的,当然你可以用其它的语言,如JSP,C#之类…… 验证通过的截图

20150302225156

这里我把PHP代码贴出来
valid();
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)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                libxml_disable_entity_loader(true);
              	$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 function checkSignature()
	{
        // you must define TOKEN by yourself
        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);
        // use SORT_STRING rule
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
}
?>
是不是非常的简单呢,后面自定义菜单和推送数据慢慢再帖出来!

本文链接:https://www.it72.com/1193.htm

推荐阅读
最新回复 (1)
返回