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:
Because many developers will encounter the following situations when developing:
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
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.
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 接口进行分享 }
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:
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!