Home  >  Article  >  WeChat Applet  >  Analysis of verification steps examples for WeChat public platform development (PHP version)

Analysis of verification steps examples for WeChat public platform development (PHP version)

怪我咯
怪我咯Original
2017-04-06 13:28:441700browse

This article mainly introduces the verification steps for the development of the php version of the WeChat public platform. It analyzes in detail the operation steps and related parameter meanings of the PHP WeChat public platform verification in the form of examples. Friends in need can refer to the following

The example in this article describes the verification steps for the development of the PHP version of WeChat public platform. I share it with you for your reference. The details are as follows:

We have done a lot of WeChat public platform development now. Here we introduce to you an entry-level basic knowledge of WeChat public platform verification. Those who are interested and Let me take a look.

When developing WeChat, you need to verify it. Where can you download the source code in the official developer center? After logging in to the public account, you will see a developer center in the bottom corner on the left. Click on it, and then fill in your corresponding The Token and url can then be verified successfully and then developed.

Download the WeChat PHP verification source code in the Developer Center - Developer Documentation - Interface Message - Verify that the message is true - Pull Go to the bottom and see the php demo code.

After downloading, the code is as follows:


<?php
/**
 * wechat php test
 * update time: 20141008
 */
//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 (!emptyempty($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(!emptyempty( $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, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>


Among them: TOKEN is changed to If you want it, you should write the same thing in the developer center. During the verification process, $wechatObj->valid(); this code cannot be removed. This is verification. After the verification is successful, we can put this $wechatObj ->valid(); Comment this out, and then use $wechatObj->responseMsg(); for testing

Note: You need to put $wechatObj->valid( during development ); Comment it out, otherwise nothing will be displayed when testing on a mobile phone.

The above is the detailed content of Analysis of verification steps examples for WeChat public platform development (PHP version). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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