Hello, I can not get the username via vk_api

$pz = 'сайт.ру'; $url = "https://oauth.vk.com/access_token?client_id=******&client_secret=***&code=".$_GET['code']."&redirect_uri=".$pz; $ch = curl_init(); $timeout = 5; curl_setopt ($ch,CURLOPT_URL,$url); curl_setopt ($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $file_contents = curl_exec($ch); curl_close($ch); $decode=json_decode($file_contents); 

so I get access_token and user id later:

 $url = "https://api.vk.com/method/users.get?uid=".$decode->user_id."&fields=uid,first_name,last_name,nickname,screen_name,sex,bdate,city,country,timezone,photo,photo_medium&access_token=".$decode->access_token; $ch = curl_init(); curl_setopt ($ch,CURLOPT_URL,$url); curl_setopt ($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $file_contents = curl_exec($ch); curl_close($ch); $decode2 = json_decode($file_contents); echo $decode2->uid; 

Here is what I get:

Notice: Undefined property: stdClass :: $ uid in /usr/home/lars/htdocs/cod.php on line 23

Although var_dump perfectly shows everything.

  • @keis; To format a code, select it with the mouse and click on the {} button of the editor. - Nicolas Chabanovsky
  • What does var_dump( $decode) and var_dump( $decode2) ? - Sergiks
  • var_dump ($ decode): object (stdClass) # 1 (3) [ 122090639)} var_dump ($ decode2): object (stdClass) # 2 (1) {["response"] => array (1) {[0] => object (stdClass) # 3 (3) {["uid" ] => int (122090639) ["first_name"] => string (12) "Iznaur" ["last_name"] => string (22) "Bexultans"}}} I simply removed the values ​​of fields for example, so that less data hatching - keis
  • I tried through XML to get the answer all the same. var_dump shows everything perfectly and echo this time does not even give an error - keis

1 answer 1

Forgot "response":

 echo $decode2->response[0]->uid;