Home > Backend Development > PHP Tutorial > Detailed explanation of verification steps for PHP version of WeChat public platform development (must read)

Detailed explanation of verification steps for PHP version of WeChat public platform development (must read)

墨辰丷
Release: 2023-03-28 21:18:01
Original
1637 people have browsed it

This article mainly introduces the verification steps for the development of the PHP version of 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

We have done a lot of WeChat public platform development now. Here I will introduce to you an entry-level basic knowledge of WeChat public platform verification. If you are interested, come and take a look with the editor.

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 the Your corresponding Token and url can then be verified and then developed.

Download the WeChat PHP verification source code in the Developer Center - Developer Documentation - Interface Message - Verification Message is True - Pull to the bottom Just 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;
}
}
}
?>
Copy after login

Among them: Change TOKEN to what you want and then in the developer The center should also write the same thing. During the verification process, the code $wechatObj->valid(); cannot be removed. This is verification. After the verification is successful, we can add the comment $wechatObj->valid(); Lost it, and then use $wechatObj->responseMsg(); to test

Note: You need to comment out $wechatObj->valid(); during development, otherwise it will not work on the mobile phone Nothing will be displayed during the test.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related recommendations:

php Detailed explanation of the four methods of parsing xml

phparray_multisort Detailed explanation and examples of sorting arrays

Summary of password encryption solutions in PHP

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

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