用于接受微信小程序传入的code获取openid代码

微信小程序 php 文章 2023-01-26 15:57 761 0 全屏看文

AI助手支持GPT4.0

<?php
    $appid = "YOUR_APPID";
    $secret = "YOUR_SECRET";
    $code = $_GET['code'];
    $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
    $result = file_get_contents($url);
    $result = json_decode($result, true);
    $openid = $result['openid'];
    echo $openid;
?>

注意事项:


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

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

这个代码是通过接受微信小程序传入的code,发送请求到微信服务器,获取用户的openid。

如果你还需要获取其他信息可以在$result数组中获取。


-EOF-

AI助手支持GPT4.0