Home > Backend Development > PHP Problem > How to solve the problem of PHP WeChat public account sharing failure

How to solve the problem of PHP WeChat public account sharing failure

PHPz
Release: 2023-04-12 19:06:26
Original
1568 people have browsed it

Recently, many netizens have reported that WeChat public accounts developed using PHP will fail to share when sharing. This problem has always existed, and no clear solution has been found. Let's discuss the reasons for this problem and how to solve it.

1. Reasons for the problem

In the WeChat public account developed with PHP, we usually use the WeChat JS interface for sharing operations. When calling the WeChat JS interface for sharing, we need to meet the following two conditions:

  1. The WeChat JS interface must be called within the WeChat client and cannot be called externally;
  2. WeChat The JS interface requires the HTTPS protocol to be called.

Because many developers will encounter the following situations when developing:

  1. The HTTP protocol is used when calling the WeChat JS interface;
  2. When calling the WeChat JS interface, it is not determined whether it is called within the WeChat client.

Both of these problems will cause the sharing of WeChat official accounts to fail, because WeChat does not allow calling the WeChat JS interface within HTTP or non-WeChat clients.

2. How to solve

  1. Use HTTPS protocol

When calling the WeChat JS interface, you must use the HTTPS protocol, which is officially stipulated by WeChat. Therefore, if your website is still using HTTP protocol, you need to upgrade it to HTTPS protocol.

If you have not purchased an HTTPS certificate, you can obtain it through some free certificate authorities. Currently, the more commonly used free certificate authorities include Let's Encrypt and Cloudflare.

  1. Determine whether it is called inside the WeChat client

When calling the WeChat JS interface, you must determine whether it is currently called inside the WeChat client. Generally, we can judge by judging whether window.navigator.userAgent contains "MicroMessenger".

The following is a sample code:

if (window.navigator.userAgent.indexOf("MicroMessenger") === -1) {
    alert("请在微信客户端内部访问本页面");
} else {
    // 调用微信 JS 接口进行分享
}
Copy after login

If it is not inside the WeChat client, a prompt will pop up asking the user to go to the WeChat client to access.

3. Summary

Through the above analysis and solutions, we can draw the following conclusions:

  1. In the WeChat public account developed using PHP, call The WeChat JS interface fails to perform sharing operations, usually due to the use of the HTTP protocol or failure to determine whether it is called within the WeChat client;
  2. The way to solve this problem is to use the HTTPS protocol to make the call and at the same time determine the current Whether it is called inside the WeChat client;
  3. If there are other reasons causing the sharing to fail, you can find solutions in the WeChat development documentation.

I hope the above content will help you solve the problem of PHP WeChat public account sharing failure.

The above is the detailed content of How to solve the problem of PHP WeChat public account sharing failure. For more information, please follow other related articles on the PHP Chinese website!

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