A preliminary study on the development of WeChat development platform

黄舟
Release: 2017-04-15 11:17:26
Original
1563 people have browsed it

WeChat and the open platform that followed WeChat have become really popular recently. I have always wanted to try out a WeChat platform. I recently had a good idea and wanted to implement it as soon as possible. Fortunately, WeChat does not require any UI or other design, it only needs to show the logic. In fact, if the WeChat public platform is only used as a platform for publishing information, it is "Edit mode "In fact, there is no need to type code. It is just that in the developer mode, developers need to design certain logic and code to achieve specific functions. Next, let’s talk about the start of development:

1. Develop WeChat The public platform must first have server resources. Of course, the editing mode does not count. The so-called editing mode is a public account that simply pushes a message every day. Server resources are available in various clouds and servers. BAE/ is recommended. SAE/Nuts/Alibaba, etc., each has its own advantages, and due to competition, some free resources will be given away, which is enough to practice

##. #a. Apply for a BAE account and create an application. If it is a test, go to the homepage of the WeChat public platform to download the test

PHP code, and just change the TOKEN to your own TOKEN, such as the PHP below. Test code, the logic of the code is very clear, that is, a verification process, and can also define part of the logical operations Upload this file and create version 1 of your own application:

<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "weixin");//此时你的微信公众平台的token即为weixin
$wechatObj = new wechatCallbackapiTest();
$wechatObj->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)){
                
              	$postObj = simplexml_load_string($postStr, &#39;SimpleXMLElement&#39;, 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 ))
                {
              		$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()
	{
        $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;
		}
	}
}

?>
Copy after login

The new version was created successfully. As shown in the figure, the application is created successfully:

A preliminary study on the development of WeChat development platform

2. Enter the developer mode of the WeChat public platform

A preliminary study on the development of WeChat development platform

You need to verify separately to become a developer mode. Please note that the URL in the picture below must be filled in correctly, which is the URL of a platform application you just applied for. Remember to be accurate. URL, otherwise an error "404 NOTFOUND" will be reported

A preliminary study on the development of WeChat development platform

A preliminary study on the development of WeChat development platform##Slide the upper right corner to enable the

button

.

##You can successfully enable the development mode

A preliminary study on the development of WeChat development platformAt this point, you can define various functions and logic in the code to meet your needs and start the magical WeChat public platform. trip.

The above is the detailed content of A preliminary study on the development of WeChat development platform. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template