In general, I have a blog in php, but in api VKontakte I get lost, so I ask here. How can I get my personal avatar and status from VK to my blog? So that when you change them in VKontakte, they also changed on the blog. I think many bloggers are interested in how to do this.
3 answers
- Create an iframe application
- Download APIServerPHPClass.zip
- There is a file vkapi.class.php , add this file to your site.
Next we get our profile data VK:
require 'vk/vkapi.class.php'; #путь к файлу vkapi.class.php $api_id = '0000'; #id приложения $secret_key = 'secret'; #секретный ключь приложения $user_id = '1'; #Ваш ID ВКонтакте //включаю библиотеку VK $VK = new vkapi($api_id, $secret_key); $prof = $VK->api('getProfiles', array('uids' => $user_id, 'fields' =>'first_name,last_name,photo_100,status,screen_name')); $adsd = sizeOf($prof['response']); for ($d = 0; $d < $adsd; $d++) { echo '<ul id="id__user__profile"> <li> <img src="' . $prof['response'][$d]['photo_100'] . '"> <a href="http://vk.com/' . $prof['response'][$d]['screen_name'] . '" class="user-name">' . $prof['response'][$d]['first_name'] . ' ' . $prof['response'][$d]['last_name'] . '</a> <span class="user-group">' . $prof['response'][$d]['status'] . '</span> </li> </ul>';}
CSS (Mini Profile Style): profile.css
Example: http://bit.ly/10OHg9e
- and how you stuffed it all so beautifully in html? Share the output code if I’m not willing, php I mean - ochumelec
- @ochumelec, what is your blog engine? do you use CMS? (WordPress, Joomla, etc.) to install my code, you should at least be able to upload files to the file manager of your site where PHP support is needed. - Maqsood
- I have anchorcms, I’m a beginner programmer at all, and I understand how and where to insert it, only I don’t know how to write the code myself from scratch - ochumelec
- @ochumelec, I changed the code above (my own), and added a link to the CSS styles for the block, copy all the code from the vk.cc/1vLNWj page, and paste (add) the file of your site into the CSS. And PHP code, paste where there should be a block with your VC data, and do not forget to change the uids to your VK id. - Maqsood
- thank you so much! - ochumelec
No authorization required. Use the users.get
method.
More about fields: Description of fields fields
Example link: http://api.vkontakte.ru/method/users.get?uids=ID_OR_NICK&fields=photo_200,status
In response will come json
this type:
{ "response": [{ "uid": UID, "first_name": "Имя", "last_name": "Фамилия", "photo_200": "URL фотографии", "status": "Статус" }] }
An example of getting in PHP:
$request = 'http://api.vkontakte.ru/method/users.get?uids=ID&fields=photo_200,status'; $response = file_get_contents($request); $info = array_shift(json_decode($response)->response); var_dump($info->photo_200); // URL фотографии var_dump($info->status); // Статус
An example of getting on JS with jQuery:
$.getJSON('http://api.vkontakte.ru/method/users.get?uids=ID&fields=photo_200,status&callback=?', function(resp){ console.log(resp.response[0].photo_200); console.log(resp.response[0].status); });
- @ochumelec, where and how to display - completely in your hands - xEdelweiss
- Yes, this is understandable, but how can you parse this json? I'm in js nibumbum - ochumelec
- @ochumelec, did not pay attention to the language. Added by js. - xEdelweiss
- one@ochumelec, here it is better to read the basics - do not need much time, but expand the horizons. - xEdelweiss
With the status is simple. The request link will be as follows: https://api.vk.com/method/status.get?uid=#ID#&access_token=#TOKEN# Where # ID # is the user id, # TOKEN # is the access key, as a result of passing authorization process. Here is the code in which you can get the status:
$status_get = "https://api.vk.com/method/status.get?uid=#ID#&access_token=#TOKEN#"; $statusResponce = file_get_contents($status_get);
A bit of preg_match and your status text. Important! To call this method, the application must have the rights: activity. With photos similarly, but there even a token is not needed.
- and how to get a token? And how much is enough for it? I just need to display my avatar and blog status, and that they change when I change them in VK. I do not need any applications. - ochumelec
- The application must be created in any way. Just create a standalone app. About him you just need an id. Then go to the authorization link: oauth.vk.com/ ... where # APP_ID # is the id of the Standalone application. in the address bar will be a token - Rincver