当前位置: 代码迷 >> 综合 >> 用easywechat获取open_id(thinkphp5.0)
  详细解决方案

用easywechat获取open_id(thinkphp5.0)

热度:41   发布时间:2023-10-20 09:07:41.0
<?php
namespace app\phone\controller;
use think\Controller;
use think\Session;
use EasyWeChat\Foundation\Application;
class VipController extends Controller{
    //验证缓存openidprivate function hasOpenid($app){
    if(Session::get('openid')){return true;}if(input("?code")){$user = $app->oauth->user();Session::set('openid',$user->id);return true;}return false;}//获取openidpublic function get_openid(){
    
$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];     //当前页面的url$app = third_party($url);//这个是开放平台的配置,请用自己的配置if(!$this->hasOpenid($app)){return $response = $app->oauth->scopes(['snsapi_base'])->redirect();}$open_id = Session::get('openid');return $open_id;}}}