给我一段php代码,用于微信小程序根据access_token获取openid。

微信小程序 php 文章 2023-01-26 14:28 508 0 全屏看文

AI助手支持GPT4.0

<?php
    // 获取access_token
    $access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=YOUR_APPID&secret=YOUR_SECRET";
    $access_token_json = file_get_contents($access_token_url);
    $access_token_array = json_decode($access_token_json, true);
    $access_token = $access_token_array['access_token'];

    // 获取openid
    $openid_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=YOUR_OPENID&lang=zh_CN";
    $openid_json = file_get_contents($openid_url);
    $openid_array = json_decode($openid_json, true);
    $openid = $openid_array['openid'];
    echo $openid;
?>

注意事项:

  • 替换 YOUR_APPID 和 YOUR_SECRET 为你的小程序的 appid 和 secret。

  • 替换YOUR_OPENID为你的openid

  • 使用前请确保已经开启了 PHP 的 file_get_contents 函数。


-EOF-

AI助手支持GPT4.0