search
HomePHP FrameworkThinkPHPDetailed explanation of how to introduce ThinkWechat.php in TP and print logs

The following tutorial column of thinkphp framework will introduce to you how to introduce ThinkWechat.php in TP and how to print logs to log files. I hope it will be helpful to friends in need!

Interactive message development for WeChat public accounts based on Thinkphp6

After watching thinkPHP practice, I downloaded the code in the book from github and prepared to run it A program developed for WeChat public accounts.
However, because the book uses ThinkPHP3.2.3, and the latest version is already 6.0.X, I am not familiar with ThinkPHP anyway, so I downloaded the latest version to use. I expected that there would be problems running the program because of the different versions. What I think is that we should solve each problem one by one. Unexpectedly, I encountered a lot of difficulties and it took me two days to get the program running. Finally, I changed a little bit of code in the framework.

Without further ado, let me list the difficulties I encountered in turn.

How to introduce ThinkWechat.php in TP

In the book, ThinkWechat.php is placed under /Application/Home/Library. But TP6 no longer has Application, so I created a new library directory under /app and put the files in it.

You need to introduce this file in Index.php

use app\library\ThinkWechat;

Add namespace## in the ThinkWechat.php file

#namespace app\library;

Class 'app\library\SimpleXMLElement' not found

At the beginning, Baidu said that php7 should be installed on the environment- xml. My macmini needs to be installed with brew. I haven’t used brew for a long time. Brew update is extremely slow. According to Baidu’s post, I replaced the link to Alibaba Cloud.

The result still doesn’t work. I brewed it for several hours. I don’t know how I found out later that namespace was used in tp6, so when using SimpleXMLElement, you have to write the following statement at the beginning of the file

use SimpleXMLElement;

How about TP6 Print the log to the log file

Because I interact with the WeChat official account, I don’t know how to facilitate debugging. I tried the interface debugging tool provided by WeChat, but it can only check whether the parameter settings are correct. So I used the stupidest method of printing logs.

To print logs, you need to make the following settings in TP6:

In config/log.php

1. Set the logging level

'level' => [' emergency'],

I wrote almost all level values ​​here when debugging.

1. Set the log saving directory

'path' => App()->getRuntimePath() .'/log',2. Then use the following in the program The statements are written to the log file in real time

Log::write('index _get session id before set ID '. Session::getId(), 'notice');

After following the test public account, enter 999 to receive a normal reply. Enter 1 and the program will exit

This problem stuck with me for most of the day! !

At first, I thought there was a problem with the response returned to the WeChat platform, because I printed a lot of logs in ThinkWechat.php and found that as long as I entered information other than 999, the data2xml method could not be fully executed. , the log cannot be printed after


$node = dom_import_simplexml($child);.

I always thought it was

$node->appendChild($node->ownerDocument->createCDATASection($value));There was a problem with the execution of this code.
Therefore, I also found from the book "WeChat Public Platform Development" that the response xml is generated by setting the xml template and replacing the variables in the template with the sprintf method.

The result of this is that I can see in the log that the response xml was successfully generated, but entering 1 still did not get the response I expected. It should be that there is no response, and it displays "This official account is temporarily unable to provide services." , please try again later", and the program also exited immediately.

Later I discovered that if I randomly wrote a program, such as a public account that responded to the information entered by the user every time, like a response bucket, there would be no problems and the program would not exit. I suddenly wondered if it might be related to session? Because session is used in the source code to implement user registration and login.

I looked at the development documentation of TP6, and sure enough, it says that the session is not initialized by default. I feel like crying here.

According to the documentation, I opened the session:

1. Remove the comment

\think\middleware\SessionInit::class in app/middleware.php.

2. And remove session_start() in the code. Because TP6 only supports operating sessions through Session class methods and session helper functions. It does not support all session_xx functions.

After the Session is opened, the program will no longer exit. But a new problem occurred. After entering 999, enter 1. The official account responded correctly. Please enter the user name, but the reply after entering the user name was the same as entering 999. When prompted, enter 1 to register and enter 2 to log in. The correct one should be to prompt for a username.

At this time, I found that multiple session files were generated in the runtime/session directory. For the same user, there should be only one session file that is correct. It is equivalent to a new session file being generated every time the user interacts with the official account, so that the information previously entered by the user cannot be obtained.

Baidu later discovered that the reason for this is that

session is stored on the server side, so to distinguish each user's session, you need to use the client's cookie. The WeChat server does not send cookies to the developer server. Therefore, cookie-based sessions cannot be used.
But as long as a unique session_id is set for each user, the same effect can be achieved.
Everyone's WeChat ID is unique, so we can use WeChat ID as the user's session_id, or we can use it after md5 encryption.

I plan to use the value of FromUserName as the sessionid. That is, each user's own openid. However, TP6 does not support the session_id() method to set the sessionid. I later read the documentation and found that Session::setId can be used to set the SessionID, but I don't know why a different sessionID is still generated every time.

It says on the Internet that you can use openid. I found that every time the URL sent by the WeChat public account to the server does come with openid, I configured openid in session.php, hoping that TP6 will use the openid in the request every time. as sessionid. But it still didn't take effect.

TP6's session.php has the following configuration:

SESSION_ID submission variable to solve flash upload cross-domain
'var_session_id' => 'openid',

Checked the setId of the framework, the original code is as follows

public function setId($id = null): void
    {
        $this->id = is_string($id) && strlen($id) === 32 && ctype_alnum($id) ? $id : md5(microtime
        (true).session_create_id());
    }

It turns out that the sessionid must be a 32-bit string containing alphanumeric characters. If it does not meet the requirements (openid length is 28 bits), use The current time is used as the sessionid, so I changed the setId to the following, and then printed the sessionid in the index method, and found that it is the same every time.

public function setId($id = null): void
    {
        $this->id = is_string($id) && strlen(md5($id)) === 32 && ctype_alnum(md5($id)) ? md5($id) : md5(microtime
        (true).session_create_id());
    }

Run the program and everything works fine. It feels great.

At this time, I think that all the problems I encountered before should have nothing to do with xml response. So I used the previous ThinkWechat.php, but found that the session file was not generated in the runtime/session directory.

After checking, I found that the response method of the original ThinkWechat.php ended with

exit($xml->asXML());

The TP6 official document has the following reminder:

Note that the operation of writing data to the Session will be localized and stored uniformly at the end of the request, so do not use exit and other interrupt operations after writing the Session data, which may This will cause the Session to not be written normally.

So I changed the exit statement to return $xml->asXML(); At this time, the session file is generated normally, and the information replied by the official account is also correct.

PS code has been hosted on github

https://github.com/sarawang9012/thinkwechat

Related recommendations: The latest 10 thinkphp video tutorials

The above is the detailed content of Detailed explanation of how to introduce ThinkWechat.php in TP and print logs. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
What Are the Key Features of ThinkPHP's Built-in Testing Framework?What Are the Key Features of ThinkPHP's Built-in Testing Framework?Mar 18, 2025 pm 05:01 PM

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?Mar 18, 2025 pm 04:57 PM

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?Mar 18, 2025 pm 04:54 PM

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?Mar 18, 2025 pm 04:51 PM

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

What Are the Advanced Features of ThinkPHP's Dependency Injection Container?What Are the Advanced Features of ThinkPHP's Dependency Injection Container?Mar 18, 2025 pm 04:50 PM

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

How to Use ThinkPHP for Building Real-Time Collaboration Tools?How to Use ThinkPHP for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:49 PM

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?Mar 18, 2025 pm 04:46 PM

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?Mar 18, 2025 pm 04:45 PM

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),