3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * 公共控制器 * Created by Mini * Time:2014-10-17 15:52:10 * * ┏┓ ┏┓ * ┏┛┻━━━━━━┻┗┓ * ┃ ┃ * ┃ ━ ┃ * ┃ ┳┛ ┗┳ ┃ * ┃ ┃ * ┃ ┻ ┃ * ┗━┓ ┏━┛ * ┃ ┃ 神兽保佑,代码无忧! * ┏━━━┛ ┃ 从此代码再无BUG! * ┏┫ ┃ ORZ ORZ ORZ ORZ....... * ┗┓ ┃ * ┗┓┏┳━━━┓ ┏┏┛ * ┣┣┃ ┣┣┃ * ┗┻┛ ┗┻┛ * */ class CreateOauth { protected $appid; //公众平台AppID protected $appsecret; //公众平台AppSecret; protected $curl_timeout; //CRUL超时时间 默认30S protected $code; //Oauth认证 回调Code protected $flag; //Oauth认证的模式 //false-->不弹出授权页面,直接跳转,只能获取用户openid //true-->弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息 protected $access_token; public function __construct ($appid = '', $appsecret = '', $curl_timeout = 30, $flag = false) { if ($appid == '' || $appsecret == '') { return false; } $this->appid = $appid; $this->appsecret = $appsecret; $this->curl_timeout = $curl_timeout; $this->flag = $flag; } /** * 作用:生成可以获得code的url */ public function createOauthUrlForCode ($redirectUrl = '') { if ($redirectUrl == '') return false; $urlObj['appid'] = $this->appid; $urlObj['redirect_uri'] = urlencode($redirectUrl); $urlObj['response_type'] = 'code'; if ($this->flag) { $urlObj["scope"] = "snsapi_userinfo"; //弹出授权页面 } else { $urlObj["scope"] = "snsapi_base"; //不弹出授权页面 } $urlObj["state"] = "STATE"."#wechat_redirect"; $bizString = $this->formatPara($urlObj); $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?'.$bizString; return $url; } /** * 作用:生成可以获得openid的url */ public function createOauthUrlForOpenid () { $urlObj["appid"] = $this->appid; $urlObj["secret"] = $this->appsecret; $urlObj["code"] = $this->code; $urlObj["grant_type"] = "authorization_code"; $bizString = $this->formatPara($urlObj); return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString; } /** * 作用:生成可以获得用户详细信息的URL */ public function createUrlForUserInfo () { $urlObj["access_token"] = $this->access_token; $urlObj["openid"] = $this->openid; $urlObj["lang"] = 'zh_CN '; $bizString = $this->formatPara($urlObj); return "https://api.weixin.qq.com/sns/userinfo?".$bizString; } /** * 作用:设置code */ public function setCode ($code_) { $this->code = $code_; } /** * 作用:通过curl向微信提交code,以获取openid */ public function getOpenid () { $url = $this->createOauthUrlForOpenid(); //初始化curl $ch = curl_init(); //设置超时 curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //运行curl,结果以jason形式返回 $res = curl_exec($ch); curl_close($ch); //取出openid $data = json_decode($res,true); $this->openid = $data['openid']; $this->access_token = $data['access_token']; return $this->openid; } /** * 作用:通过curl获取用户信息 */ public function getUserInfo () { $this->getOpenid(); $url = $this->createUrlForUserInfo(); //初始化curl $ch = curl_init(); //设置超时 curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //运行curl,结果以jason形式返回 $res = curl_exec($ch); curl_close($ch); //取出openid $data = json_decode($res,true); return $data; } /** * 作用:生成参数 */ protected function formatPara ($paraMap) { $buff = ''; foreach ($paraMap as $k => $v) { $buff .= $k . '=' . $v . '&'; } $reqPar = ''; if (strlen($buff) > 0) { $reqPar = substr($buff, 0, strlen($buff)-1); } return $reqPar; } }
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/vaDWO on line 27
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/vaDWO on line 27
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/vaDWO on line 27
Process exited with code 255.

preferences:
90.82 ms | 410 KiB | 5 Q