Advertisement
KiberInfinity

vk api auth&online

Mar 15th, 2013
1,574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.94 KB | None | 0 0
  1. <?php
  2. $login = 'login'; //Логин
  3. $pass = 'pass'; //Пароль
  4. $app = '2274003'; //id приложения для получения token
  5. $key = 'hHbZxrka2uZ6jB1inYsH'; //secret key приложения
  6.  
  7.  
  8. $acc_token_file='vk_acc_token.php';
  9. $access_token = '';
  10.  
  11. load_token();
  12. api('account.setOnline');
  13.  
  14. function load_token($reauth=false){
  15.    global $access_token,$acc_token_file;
  16.    if (!$reauth && file_exists($acc_token_file)) {
  17.       $s=file_get_contents($acc_token_file);
  18.       preg_match("/\[([a-f0-9]+)\]/", $s, $matches);
  19.       if (!$matches[0]){
  20.          load_token(true);
  21.       } else {
  22.          $access_token=$matches[1];
  23.       }
  24.    } else {
  25.       auth_api();
  26.       if ($access_token==""){
  27.          echo "Auth Error";
  28.       } else {
  29.          file_put_contents($acc_token_file,'<?php if (!defined("vk_online")) die("x_X"); "['.$access_token.']";?>');
  30.       }
  31.    }
  32. }
  33.  
  34.  
  35. function curl( $url ) {
  36.         $ch = curl_init( $url );
  37.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  38.         curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
  39.         curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );        
  40.         $response = curl_exec( $ch );
  41.         curl_close( $ch );
  42.         return $response;
  43. }
  44. function auth_api(){
  45.    global $access_token,$app,$key,$login,$pass;
  46.    $auth = curl( "https://oauth.vk.com/token?grant_type=password&client_id=$app&client_secret=$key&username=$login&password=$pass" ); //Авторизация
  47.    $json = json_decode( $auth, true );
  48.    $access_token = $json['access_token'];  
  49. }
  50. function api($method){
  51.    global $access_token;
  52.    $r = curl("https://api.vk.com/method/$method?access_token=$access_token");
  53.    $json = json_decode( $r, true );
  54.    if ($json['error']){
  55.       $code=$json['error']['error_code'];
  56.       if ($code==4 || $code==3){
  57.          load_token(true);
  58.          api($method);
  59.       } else {
  60.          echo $r;
  61.       }
  62.    } else {
  63.       echo $r;
  64.    }
  65. }
  66.  
  67. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement