Home > php教程 > php手册 > php 微信开发平台开发小试验

php 微信开发平台开发小试验

WBOY
Release: 2016-06-21 08:48:51
Original
1204 people have browsed it

微信和随之微信产生的开放平台最近着实火了一下。一直想做个微信的平台试试,最近有了个好点子,想尽快把它实现出来,好在微信不需要什么UI等等的设计,只需要把逻辑展现出来即可了。其实微信公众平台如果只是作为一个发布信息的平台的话,也就是“编辑模式”下其实是无需敲代码的。只是在开发者模式下需要开发者去设计一定的逻辑和代码去实现特定的功能,接下来就说下开发的起步:

 

1.开发微信公众平台首先要有服务器资源,当然那种编辑模式的不算。所谓的编辑模式就是那种单纯的每天推送一条消息的公众账号。服务器资源各种云各种服务器都是可以的,推荐BAE/SAE/坚果/阿里等等的一种,各有好处,而且由于竞争的缘故?都会有部分免费的资源相送,拿来练练手就足够的了。

a.申请BAE账号,创建应用,如果是测试的话去微信公众平台的主页下载测试的PHP代码,只需将其中的TOKEN修改成自己的TOKEN即可。例如下面的PHP测试代码,代码的逻辑很清晰,即一个验证过程,也能定义一部分的逻辑操作。上传这个文件并且创建自己应用的版本1:

 

 

<?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"; (PS:^_^不错的php学习交流群:276167802,验证:csl,有兴趣的话可以加入进来一起讨论)  
$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

 



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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template