小程序受權代碼示例

 public function authlogin(){ 數據庫

if($_GET){
            $appid = ''; //微信的appid
            $secret= ''; //微信的secret祕鑰
            $code= $_GET['code'];//小程序傳來的code值
            $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code"; //請求接口獲取openid
            $open = $this->http_curl($url);
            //yourAppid爲開發者appid.appSecret爲開發者的appsecret,均可以從微信公衆平臺獲取
            $openid = $open['openid'];
            $sid = $_POST['sid'];//邀請人id
            $where['openid']=$openid;
            $list=M('user')->where($where)->find();
            if ($list !=null) { //若是數據庫中存在此用戶的信息,則不須要從新獲取
                // $list['status']=1;
                 $arr=array('msg'=>'已經受權過了','code'=>'0','status'=>'1','list'=>$list,'openid'=>$openid);
                echo json_encode($arr,JSON_UNESCAPED_UNICODE);
            }else {
                $arr=array('msg'=>'尚未受權','code'=>'0','status'=>'0','list'=>$list,'openid'=>$openid);
                echo json_encode($arr,JSON_UNESCAPED_UNICODE);
            }

        }else{
            $arr=array('msg'=>'請求錯誤','code'=>'1');
            echo json_encode($arr,JSON_UNESCAPED_UNICODE);
        }json

}小程序

   public function http_curl($url){
        //用curl傳參
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

        //關閉ssl驗證
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);


        curl_setopt($ch,CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }api

相關文章
相關標籤/搜索