Home  >  Article  >  Backend Development  >  How does the mini program obtain openid and session_key?

How does the mini program obtain openid and session_key?

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-07-15 16:10:142410browse

Do you know openid and session_key? Today I will take you to learn how to use PHP to obtain the openid and session_key of the mini program. Friends in need can refer to it.

How does the mini program obtain openid and session_key?

<?php
//获取openid
function getopenid(){//获取用户ID
    //code为前端通过 wx.login() 方式获取
    $code = $_GET["code"];
    //小程序微信官方后台 appid 值
    $appid=&#39;wx68d7c348c17f010c&#39;;
    //同样通过小程序后台获取的 APPSecret 值
    $secret=&#39;2e7840670256a9337f4d910e935f3e4b&#39;;

    $c= file_get_contents("https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$secret."&js_code=".$code."&grant_type=authorization_code");

    echo json_encode($c);
}

//返回内容 例
/*
{
    session_key: "v5M+ikxl7Ms1Jya+MjqXIg==",
    openid: "o_SM351Q-ZIA5URgTISkPvx2cSWg"
}
*/
?>

Recommended learning: php video tutorial

The above is the detailed content of How does the mini program obtain openid and session_key?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete