Public open Platform Development---Public platform PHP interface vs. Java interface
Public open Platform Development---Public platform PHP interface vs. Java interface
Technical QQ Exchange Group: javadream:251572072
-----------------------------------
The code in this section is collected in the user's production
-------------------------------------------
Version a.java-jsp
Interface code for b.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 is 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 = "
??????? %s
??????? %s
??????? %s
??????? %s
??????? %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 ()
? {
??????? $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;
??}
?}
}
?>
--------------------------------------------------
C.java Version Interface
<[email protected] import= "Java.util.Date"%>
<[email protected] import= "Org.dom4j.Element"%>
<[email protected] import= "Org.dom4j.DocumentHelper"%>
<[email protected] import= "Org.dom4j.Document"%>
<[email protected] import= "Java.io.IOException"%>
<[email protected] import= "Java.io.InputStreamReader"%>
<[email protected] import= "Java.io.BufferedReader"%>
<[email protected] import= "Java.io.Reader"%>
<[email protected] import= "Java.security.MessageDigest"%>
<[email protected] import= "Java.util.Arrays"%>
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%>
<%
//weixinhandler cannot use non-final type objects for internal classes
? final String token= "Weixin";
? final HttpServletRequest Final_request=request;
? final HttpServletResponse Final_response=response;
%>
<%
Class weixinhandler{
? public void Valid () {
?? String echostr=final_request.getparameter ("Echostr");
?? if (null==echostr| | Echostr.isempty ()) {
??? Responsemsg ();
??} else{
??? if (This.checksignature ()) {
???? This.print (ECHOSTR);
???} else{
???? This.print ("error");????????????????????????????????????????????????????????????????????????????????????????????????????? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??????????????????????????????????????????????????????????????????????????????????????????????????????????
???}
??}
?}
?//Auto-reply content
? public void Responsemsg () {
?? String Poststr=null;
?? try{
??? Poststr=this.readstreamparameter (Final_request.getinputstream ());
??} catch (Exception e) {
??? E.printstacktrace ();
??}
?? System.out.println (POSTSTR);
?? if (Null!=poststr&&!poststr.isempty ()) {
??? Document Document=null;
??? try{
???? Document = Documenthelper.parsetext (POSTSTR);
???} catch (Exception e) {
???? E.printstacktrace ();
???}
??? if (null==document) {
???? This.print ("");
???? Return
???}
??? Element root=document.getrootelement ();
??????????? String fromusername = Root.elementtext ("Fromusername");
??????????? String tousername = Root.elementtext ("Tousername");
??????????? String keyword = Root.elementtexttrim ("Content");
??????????? String time = new Date (). GetTime () + "";
??????????? String TEXTTPL = " "+
??????" %1$s "+
??????" %2$s "+
??????" %3$s "+
??????" %4$s "+
??????" %5$s "+
??????" 0 "+
??????" ";????????????
???
??? if (Null!=keyword&&!keyword.equals (""))
??????????? {
????????? ?? String Msgtype = "text";
??????????? ? String contentstr = "Welcome to WeChat world!";
??????????? ? String resultstr = Texttpl.format (Texttpl, Fromusername, Tousername, Time, Msgtype, CONTENTSTR);
??????????? ? This.print (RESULTSTR);
??????????? }else{
??????????? ? this.print ("Input something ...");
??????????? }
???? }else {
???? ? This.print ("");
???? }
?}
?//Interface Verification
? public boolean checksignature () {
?? String signature = final_request.getparameter ("signature");
??????? String timestamp = final_request.getparameter ("timestamp");
??????? String nonce = Final_request.getparameter ("nonce");
??????? String Token=token;
??????? String[] Tmparr={token,timestamp,nonce};
??????? Arrays.sort (Tmparr);
??????? String Tmpstr=this. Arraytostring (Tmparr);
??????? Tmpstr=this. Sha1encode (TMPSTR);
??????? if (Tmpstr.equalsignorecase (signature)) {
??? return true;
??} else{
??? return false;
??}
?}
?//Send return data to the requester side
? public void print (String content) {
?? try{
??? Final_response.getwriter (). print (content);
??? Final_response.getwriter (). Flush ();
??? Final_response.getwriter (). Close ();
??} catch (Exception e) {
???
??}
?}
?//Array to string
? public string Arraytostring (string [] arr) {
?? StringBuffer bf = new StringBuffer ();
?? for (int i = 0; i < arr.length; i++) {
?? Bf.append (Arr[i]);
??}
?? return bf.tostring ();
?}
?//SHA1 encryption
? public string Sha1encode (string sourcestring) {
?? String resultstring = null;
?? try {
???? resultstring = new String (sourcestring);
???? MessageDigest MD = messagedigest.getinstance ("SHA-1");
???? resultstring = byte2hexstring (Md.digest (Resultstring.getbytes ()));
??} catch (Exception ex) {
??}
?? return resultstring;
?}
? Public final String byte2hexstring (byte[] bytes) {
?? StringBuffer buf = new StringBuffer (Bytes.length * 2);
?? for (int i = 0; i < bytes.length; i++) {
??? if (((int) bytes[i] & 0xff) < 0x10) {
????? Buf.append ("0");
???? ?}
??? Buf.append (long.tostring ((int) bytes[i] & 0xFF, 16));
??}
?? Return buf.tostring (). toUpperCase ();
?}
?//Read post parameters from input stream
? public String Readstreamparameter (ServletInputStream in) {
?? StringBuilder buffer = new StringBuilder ();
?? BufferedReader Reader=null;
?? try{
??? reader = new BufferedReader (new InputStreamReader (in));
??? String Line=null;
??? while (line = Reader.readline ())!=null) {
???? Buffer.append (line);
???????? }
??} catch (Exception e) {
??? E.printstacktrace ();
??} finally{
??? if (Null!=reader) {
???? try {
????? Reader.close ();
????} catch (IOException e) {
????? E.printstacktrace ();
????}
???}
??}
?? return buffer.tostring ();
?}
}
%>
<%
? Weixinhandler handler=new Weixinhandler ();
? Handler.valid ();
%>
-----------------------------------------
D. The above is all the code for the PHP interface and the JSP interface.
? Now let's take a look at some of the areas that need attention:
? First of all, the JSP is more cumbersome than PHP, because many functions need to write their own,
? Like SHA1 encryption, parsing XML strings and so on need to find their own third-party libraries.
? 1th, we want to get the public platform to send the JSP post or get parameters, under normal circumstances are used request.getparameter can be obtained,
? But in the process of writing found that PHP is to get
? $POSTSTR = $GLOBALS ["Http_raw_post_data"];
? This is done by querying some data to get the "unrecognized MIME type data" that cannot be obtained through the $_get or $_post function, the original POST data
? So here's how to parse the post XML data in the same way that it gets the raw data stream
? String Poststr=null;
?? try{
??? Poststr=this.readstreamparameter (Final_request.getinputstream ());
??} catch (Exception e) {
??? E.printstacktrace ();
??}
-----------------------------------------------------
Reading post parameters from an input stream
? public String Readstreamparameter (ServletInputStream in) {
?? StringBuilder buffer = new StringBuilder ();
?? BufferedReader Reader=null;
?? try{
??? reader = new BufferedReader (new InputStreamReader (in));
??? String Line=null;
??? while (line = Reader.readline ())!=null) {
???? Buffer.append (line);
???????? }
??} catch (Exception e) {
??? E.printstacktrace ();
??} finally{
??? if (Null!=reader) {
???? try {
????? Reader.close ();
????} catch (IOException e) {
????? E.printstacktrace ();
????}
???}
??}
?? return buffer.tostring ();
?}
--------------------------------------------------------------
The second, is the response message back to the platform, I tried to use the most general out.print to do, but found that no response, observe the Code of PHP syntax
echo "";
Exit
------------------
Conjecture may need to have a refresh operation in order to response the message back, so find the next response inside some of the functions to make the following attempt
Send return data to the requester side
? public void print (String content) {
?? try{
??? Final_response.getwriter (). print (content);
??? Final_response.getwriter (). Flush ();
??? Final_response.getwriter (). Close ();
??} catch (Exception e) {
???
??}
?}
---------------------------------------
A detailed description of the message interface of the public platform can be viewed in the official documentation of the public platform, which describes the format of the XML of the message and how the message is sent.