微信方法整理1

openid)) {

            return redirect('/wx/index');
        }

        $openid = $rs->openid;
        $access_token = $rs->access_token;

        $userinfo = file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN');

        $userinfo = json_decode($userinfo);

       	//下面是自己的逻辑

    }
    /**
     *  pc  端 获取 code
     *   实现 浏览器 扫描 二维码登录
     *   微信公众平台
     */
    public function getcode()
    {
        $backurl = urlencode(config('mydata.backUrl'));
      
        $appid = config('mydata.kaifang_appid');
        $url = 'https://open.weixin.qq.com/connect/qrconnect?appid=' . $appid . '&redirect_uri=' . $backurl . '&response_type=code&scope=snsapi_login&state=123#wechat_redirect';
        header("Location:" . $url);
    }

    /**
     * pc 端 回调函数
     * 微信公众平台
     */
    public function codebackurl()
    {
      
        $appid = config('mydata.kaifang_appid');
        $secret = config('mydata.kaifang_secrect');

        $code = $_GET['code'];
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code';
        $rs = file_get_contents($url);

        $rs = json_decode($rs);
      
        $access_token = $rs->access_token;

        $openid = $rs->openid;

        $userinfo = file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN');

        $userinfo = json_decode($userinfo);//自立的逻辑。。。。

    }

     /**
     * 生成二维码
     *
     */
    public static function erweima($scene_id)
    {
        

        $appId = '****';
        $secret = '******';
        //获取token
        $url1 = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appId . '&secret=' . $secret;

        $token = file_get_contents($url1);

        $token_info = json_decode($token);
        $access_token = $token_info->access_token;
        $urltick = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' . $access_token;
        $post_data = ['action_name' => 'QR_LIMIT_SCENE', 'action_info' => ['scene' => ['scene_id' => ($qrmaxid + 1)]]];
        $data = json_encode($post_data);

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $urltick);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $rs = curl_exec($ch);
        curl_close($ch);

        $ticketinfo = json_decode($rs);

        $ticket = $ticketinfo->ticket;
        $url = $ticketinfo->url;
      

        $ticket = urlencode($ticket);

        $urlimg = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . $ticket;

        $dirfile = date('Ymd');
        if (!file_exists('images/qrcode')) {
            mkdir('images/qrcode') ? "images/qrcode" : exit('上传目录创建失败');
        }

        $truename = time() . '.jpg';

        $filename = 'images/qrcode/' . $truename;

        //下载图片
        $ch = curl_init();
        $fp = fopen($filename, 'wb');

        curl_setopt($ch, CURLOPT_URL, $urlimg);
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);

        $rs = curl_exec($ch);
        curl_close($ch);
        fclose($fp);

        if ($rs) {
           //下载二维码成功 进行保存操作。。。。
        }
        return 0;
    }

    public function downloadImageFromWeixin($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);
        curl_close($ch);
        return array_merge(array('body' => $package), array('header' => $httpinfo));
    }

你可能感兴趣的:(微信)