avatar

PHP get wechat miniprogram user openid
php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$appid = '';
$secret = $this->app;
$code = $_REQUEST["code"];
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $secret . '&js_code=' . $code . '&grant_type=authorization_code';

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// To ensure communication security,all api use https,so need use two line code to open ssl safety check
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);

$res = curl_exec($curl);
curl_close($curl);

$json_obj = json_decode($res, true);
$openid = $json_obj["openid"];
$data['openid'] = $openid;
return json_encode($data);
Author: Qin
Link: https://qinhaolei.com/posts/4383/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Reward
  • WechatPay
    WechatPay
  • AliPay
    AliPay
  • Bitcoin
    Bitcoin

Comment