Home > Backend Development > PHP Tutorial > PHP cURL obtains WeChat public account access_token instance php instance

PHP cURL obtains WeChat public account access_token instance php instance

jacklove
Release: 2023-04-01 19:40:01
Original
1766 people have browsed it

This article mainly introduces the example of PHP cURL to obtain the access_token of the WeChat public account. Friends who need it can refer to it

1. To develop the WeChat public account, you must first obtain the access_token, and set it as a developer before running the code. Add this server IP to the whitelist

public function index(){
    $appId = 'wxd0e50fe967dccccc';
    $appSecret = 'd7f6be12ce41b60ss0f45054';//虚拟的,不要用
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$url); //要访问的地址 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//跳过证书验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
    $data = json_decode(curl_exec($ch));
    if(curl_errno($ch)){
      var_dump(curl_error($ch)); //若错误打印错误信息 
    }
    var_dump($data); //打印信息
    
    curl_close($ch);//关闭curl
  }
Copy after login

2. The access_token obtained is valid within 2 hours

Summary

The above is an example of PHP cURL getting the WeChat public account access_token introduced by the editor. I hope it will be helpful to everyone. , if you have any questions, please leave me a message, and the editor will reply to you in time. I would also like to thank you all for your support of the php Chinese website!

Articles you may be interested in:

PHP implements the turntable lottery algorithm sharing php examples

PHP uses curl_multi to implement concurrent requests. Example php skills

PHP performance testing tool xhprof installation and use method detailed php skills

The above is the detailed content of PHP cURL obtains WeChat public account access_token instance php instance. 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