写一篇简单的微信接入
微信接入測試URL,您可以使用這2個中的一個來進行接入測試(近期在修改中):
http://ydxiaoyuan.duapp.com/
http://ydxiaoyuan.duapp.com/test
TOken;任意數字都可以
微信付費咨詢及開發:QQ915770631
微信接入,需要有獨立服務器,并且開通80端口,否則微信服務器不會發送相應信息到您的服務器上
本鏈接忽略了TOken及其他數據驗證,各位可以放心測試,
接入部分代碼;
1servlet版本:
public class Authentication extends HttpServlet {
/**
*?
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String token = "123";
String signature = req.getParameter("signature");
String timestamp = req.getParameter("timestamp");
String nonce = req.getParameter("nonce");
String echostr = req.getParameter("echostr");
//進行驗證(略)
resp.getOutputStream().print(echostr);
super.doGet(req, resp);
}
}
2jsp版本:
<%@ page language="java" import="java.util.*,java.net.URL" pageEncoding="UTF-8"%><%
String echoStr=request.getParameter("echostr");
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//out.close();
%><%=echoStr%>
3php版本:
<?php
/**
? * wechat php test
? */
//define your token
define("TOKEN", "11111");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
public function valid()
? ? {
? ? ? ? $echoStr = $_GET["echostr"];
echo $echoStr;
? ? ? ? exit;
? ? ? ? //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 ))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? $msgType = "text";
? ? ? ? ? ? ? ? $contentStr = "歡迎來到xiaoqxuexi";
? ? ? ? ? ? ? ? $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
? ? ? ? ? ? ? ? echo $resultStr;
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? $msgType = "text";
? ? ? ? ? ? ? ? $contentStr = "歡迎來到小Q教你學!InputSometing!";
? ? ? ? ? ? ? ? $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
? ? ? ? ? ? ? ? echo $resultStr;
? ? ? ? ? ? ? ? }
? ? ? ? }else {
? ? ? ? $msgType = "text";
? ? ? ? ? ? ? ? $contentStr = "歡迎來到小Q教你學xiaoqxuexi !InputSometing!";
? ? ? ? ? ? ? ? $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
? ? ? ? ? ? ? ? echo $resultStr;
? ? ? ? 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;
}
}
}
?>
總結
以上是生活随笔為你收集整理的写一篇简单的微信接入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL Server-【知识与实战III
- 下一篇: 使用EXCEL计算并绘制RSI指标