java微信开发入门第一讲
2017-04-14 11:18
148 查看
微信开发首先必须要有一个web服务器来验证你的信息,所以我们首先要申请一个服务器
1.服务器申请
我们可以的到福佳jsp申请一个php或者Java的空间,申请哪个都可以,不影响使用,我们使用php空间,这里的验证代码在微信官网提供了,所以我们直接上传就可以了
<?php
/**
* wechat php test
*/
//define your token
define("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)){
/* 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 = "<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()
{
// 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;
}
}
}
?>
2.申请微信公众平台测试账号
打开浏览器,这里以IE为例。输入:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
填写配置信息
这样环境就配置好了,接下来我们可以编写代码了
1.服务器申请
我们可以的到福佳jsp申请一个php或者Java的空间,申请哪个都可以,不影响使用,我们使用php空间,这里的验证代码在微信官网提供了,所以我们直接上传就可以了
<?php
/**
* wechat php test
*/
//define your token
define("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)){
/* 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 = "<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()
{
// 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;
}
}
}
?>
2.申请微信公众平台测试账号
打开浏览器,这里以IE为例。输入:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
填写配置信息
这样环境就配置好了,接下来我们可以编写代码了
相关文章推荐
- Java微信开发入门
- JAVA微信开发之weixin4j入门视频
- JAVA微信开发教程入门篇 -01 开发环境搭建
- java微信开发框架wechat4j入门教程
- 微信开发从入门到精通教程大全 资料大全 java和php版本;教程文档、代码、视频 微信商城实例
- java微信开发框架wechat4j入门教程
- java微信开发框架wechat4j入门教程
- (入门菜鸟级别程序员)微信开发java遇到的难题,分享给其他新人微信开发者
- Java微信开发入门
- 微信开发从入门到精通教程大全 资料大全 java和php版本;教程文档、代码、视频 微信商城实例
- java微信公众号开发入门(2)--微信官方到本地校验
- JAVA微信开发之weixin4j入门视频
- 微信公众账号开发接口实现 - java servlet
- 微信公众账号开发教程(三) 实例入门:机器人(附源码)(转)
- 微信公共号开发教程java版——基础知识和环境搭建(一)
- 微信开发获取地理位置实例(java,非常详细,附工程源码)
- Idea开发Java WEB 应用入门教程
- 微信公共号开发教程java版——请求消息,响应消息及事件消息类的封装(三)
- 微信开发入门第四章:文章列表页面
- java微信开发API解析(五)-用户管理