secret = $secret; $this->appid = $appId; } /** * 获取用户信息 */ public function getUserInfo($code,$anonymous_code ,$encryptedData='',$iv=''){ //获取用户信息 $queryarr = [ "appid" => $this->appid , "secret" => $this->secret , 'code' =>$code, ]; $ret = $this->curl_post('https://developer.toutiao.com/api/apps/v2/jscode2session',json_encode($queryarr)); if ($ret){ //解密数据 if($encryptedData){ $key = $ret['data']['session_key']; // $ret['mobile'] = $phone['']; $phone = openssl_decrypt(base64_decode($encryptedData,true), 'AES-128-CBC', base64_decode($key), OPENSSL_RAW_DATA, base64_decode($iv)); $ret['data']['mobile'] = json_decode($phone,true)['phoneNumber']; } // print_r($phone);die; return $ret; } return []; } /** * 获取用户信息 * @param array $code * @return array */ public function getUserInfo2($code) { //获取access_token $dataK = $this->getAccessToken($code); if ($dataK['data']['error_code'] != 0){ return ["error_code"=>$dataK['data']['error_code'],"description"=>$dataK['data']['description']]; } var_dump($dataK); $data = isset($dataK['data']) ? $dataK['data'] : $dataK; $access_token = isset($data['access_token']) ? $data['access_token'] : ''; $refresh_token = isset($data['refresh_token']) ? $data['refresh_token'] : ''; $expires_in = isset($data['expires_in']) ? $data['expires_in'] : 0; // print_r($dataK);die; if ($access_token) { $openid = isset($data['open_id']) ? $data['open_id'] : ''; $unionid = isset($data['union_id']) ? $data['union_id'] : ''; //获取用户信息 $queryarr = [ "access_token" => $access_token, "open_id" => $openid ]; $ret = $this->curl_post(self::GET_USERINFO_URL, $queryarr,["Content-Type"=>"application/json"]); var_dump($ret); $userinfo = $ret['data']; $userinfo['openid'] = $userinfo['open_id']; $userinfo['unionid'] = $userinfo['union_id']; if (!$userinfo || isset($userinfo['errcode'])) { return []; } $userinfo = $userinfo ? $userinfo : []; $userinfo['avatar'] = isset($userinfo['avatar']) ? $userinfo['avatar'] : ''; $data = [ 'access_token' => $access_token, 'refresh_token' => $refresh_token, 'expires_in' => $expires_in, 'openid' => $openid, 'unionid' => $unionid, 'userinfo' => $userinfo ]; return $data; } return []; } /** * 获取access_token * @param string code * @return array */ public function getAccessToken($code = '') { if (!$code) { return []; } $queryarr = array( "client_key" => $this->appid, "client_secret" => $this->sessionKey, "code" => $code, "grant_type" => "authorization_code", ); $response = $this->curl_post(self::GET_ACCESS_TOKEN_URL, $queryarr,["Content-Type"=>"application/json"]); return $response ? $response : []; } public function curl_post($url,$post_data,$header=[]) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_HEADER, false); // 显示返回的Header区域内容 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); curl_close($ch); return json_decode($output,true); } }