Home > Backend Development > PHP Tutorial > How to access the public account in php

How to access the public account in php

爱喝马黛茶的安东尼
Release: 2023-04-07 08:14:01
Original
5029 people have browsed it

How to access the public account in php

How to access WeChat public account with PHP?

Prerequisite

·Server resources: You need to have a public network server resource (Alibaba Cloud, Tencent Cloud Or other cloud resources)

·Server environment: It is recommended to use LNMP environment, which is perfect for PHP development. (Please make sure your server has successfully run PHP. If you have any questions, you can privately chat with the poster)

·Public account resources: If you do not have a public account, first go to the public account platform to apply for WeChat public platform. Generally, individuals can only apply for a subscription number.

·Technical requirements: Master a certain amount of coding ability.

Related recommendations: "php tutorial"

Development configuration

·After logging in to the public platform Click Development->Basic Configuration.

·Get the developer password and save it (please save it where only you can see it)

·Add IP whitelist (add Go to your server’s public IP)

·Fill in the server configuration. The server address can be IP or domain name, fill in your script address (for WeChat access), customize the token, AESKEY can be randomly generated, and choose safe mode for the message method.

· Of course, please save the above information to the project to call it. Don’t click submit here yet!

Script Development

· Next, just develop the interface in the link address you filled in in the previous step.

·You can directly copy the code into it. Document description WeChat development document (the demo of the document is not connected)

<?php
namespace api\controllers;
use yii\web\Request;
class WeiXinController
{
    public function actionIndex()
    {
        $params = (new Request())->get();//不用yii2的同学替换为 $params=$_GET;
        $tmpArray = array(WX_TOKEN, $params[&#39;timestamp&#39;], $params[&#39;nonce&#39;]);//WX_TOKEN就是上一步填写的token值
        sort($tmpArray, SORT_STRING);//别漏了第二个参数
        $tmpStr = implode($tmpArray);
        $tmpStr = sha1($tmpStr);
        if ($params[&#39;signature&#39;] == $tmpStr) {
            echo $params[&#39;echostr&#39;];
            exit();
        }
        echo &#39;failed&#39;;
    }
}
Copy after login

Done

·After completing the above steps, click Submit. You will see that the configuration is successful!

·Problems that may arise.

·Token parsing failed: It means your interface did not return.

·Service error: There may be a problem with your server. Try to see if you can access the interface.

The above is the detailed content of How to access the public account in php. 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