m7v
5/26/2014 - 5:03 PM

Class VKapi release VKapi method.

Class VKapi release VKapi method.

<?php
/**
 * Created by PhpStorm.
 * User: andrew
 * Date: 5/27/14
 * Time: 12:01 AM
 */

class VKapi {

  // Get necessary data.
  public function __construct($user_id, $access_token) {
    $this->user_id = $user_id;
    $this->access_token = $access_token;
    // VK API specific.
    $this->count_files_for_multiple_upload = 5;
    $this->file_extensions = array('jpg', 'jpeg', 'png', 'gif');
  }

  // Send query using VKapi.
  private function post_to($url, $data = array()) {
    // Check that the url is provided
    if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
      throw new Exception('Invalid Url');
    }

    if (!is_array($data)) {
      throw new Exception('Data must be array');
    }
    else {
      $data = http_build_query($data);
    }

    // Send the data
    $request = "$url?$data";
    $response = file_get_contents($request);

    return json_decode($response);
  }

  // Send photo to vk server on specific url.
  private function post_file($url, $data) {
    // Check that the url is provided
    if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
      throw new Exception('Invalid Url');
    }

    if (!is_array($data)) {
      throw new Exception('Data must be array');
    }
    else {
      $data = http_build_query($data);
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);

    if (count($data) > 0) {
      curl_setopt($ch, CURLOPT_POST, TRUE);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
  }

  // Get albums list.
  public function get_albums() {
    $response = $this->post_to(
      'http://api.vk.com/method/photos.getAlbums',
      array('owner_id' => $this->user_id)
    );

    return !empty($response['response']) ? $response['response'] : FALSE;
  }

  // Get album by name.
  public function get_album_id_by_name($album_name) {
    $albums = $this->get_albums();
    if (!empty($albums)) {
      // Find album.
      foreach ($albums as $album) {
        if ($album['title'] == $album_name) {
          $album_id = $album['aid'];
        }
      }

      return !empty($album_id) ? $album_id : FALSE;
    }

    return FALSE;
  }

  // Create album and return info about new album.
  public function create_album($album_name) {
    $response = $this->post_to(
      'https://api.vk.com/method/photos.createAlbum',
      array(
        'title' => $album_name,
        'access_token' => $this->access_token,
      )
    );

    return !empty($response['response']) ? $response['response'] : FALSE;
  }

  // Save one photo in album by album_id.
  public function save_photo($album_id, $path_to_photo, $photo_comment) {
    if (!file_exists($path_to_photo)) {
      throw new Exception("File doesn't exists");
    }
    else {
      $data = array('file1' => "@$path_to_photo");
    }

    $photo_comment = str_replace(' ', '%20', $photo_comment);

    $response = $this->post_to(
      'https://api.vk.com/method/photos.getUploadServer',
      array(
        'aid' => $album_id,
        'access_token' => $this->access_token,
      )
    );

    $response = $this->post_file($response['response']['upload_url'], $data);
    $upload = drupal_json_decode($response);

    $response = $this->post_to(
      'https://api.vk.com/method/photos.save',
      array(
        'caption' => $photo_comment,
        'aid' => $album_id,
        'server' => $upload['server'],
        'photos_list' => $upload['photos_list'],
        'hash' => $upload['hash'],
        'access_token' => $this->access_token,
      )
    );

    return !empty($response['response']) ? $response['response'] : FALSE;
  }

  // Save user's photo from dir.
  public function save_photo_from_dir($album_id, $path_to_dir, $photo_comment) {
    if (!file_exists($path_to_dir)) {
      throw new Exception("Directory doesn't exists");
    }

    $list = scandir($path_to_dir);
    $data = array();
    $counter = 1;

    foreach ($list as $item) {
      $file_extension = pathinfo($item, PATHINFO_EXTENSION);
      if (in_array($file_extension, $this->file_extensions)) {
        $data[] = array("file$counter" => "@$path_to_dir/$item");
        ++$counter;
      }
      if ($counter > $this->count_files_for_multiple_upload) {
        break;
      }
    }

    $response = $this->post_to(
      'https://api.vk.com/method/photos.getUploadServer',
      array(
        'aid' => $album_id,
        'access_token' => $this->access_token,
      )
    );

    $response = $this->post_file($response['response']['upload_url'], $data);
    $upload = drupal_json_decode($response);

    $response = $this->post_to(
      'https://api.vk.com/method/photos.save',
      array(
        'caption' => $photo_comment,
        'aid' => $album_id,
        'server' => $upload['server'],
        'photos_list' => $upload['photos_list'],
        'hash' => $upload['hash'],
        'access_token' => $this->access_token,
      )
    );

    return !empty($response['response']) ? $response['response'] : FALSE;
  }

}