Retrieving photo albums from vk.com
The task was to get all photos from group albums.
First of all I needed PHP VKApi class. You can find it here: <a href="https://github.com/vladkens/VK" target="blank">vk api</a>.
The second thing I'd done was to read vk.com API docs <a href="http://vk.com/pages?oid=-1&p=%D0%9E%D0%BF%D0%B8%D1%81%D0%B0%D0%BD%D0%B8%D0%B5%D0%BC%D0%B5%D1%82%D0%BE%D0%B4%D0%BE%D0%B2API" target="blank">here</a>. I need the photos section of this docs.
To build an vk app with that php vk-api class you need:
define('APP_ID', 'YOUR_APP_ID');
define('APP_SECRET', 'YOUR_APP_SECRET_KEY');
require_once('./libs/VK.php');
$vk = new \VK\VK(APP_ID, APP_SECRET);
The next I wanted is to make it works OOP-style. I've created two classes called Photo and Album. Here they are:
class Photo {
private $id;
private $src;
private $srcBig;
private $title;
private $createdAt;
public function __construct($id = -1, $src = 'unknown', $srcBig = 'unknown', $title = 'unknown', $createdAt = -1){
$this->id = $id;
$this->src = $src;
$this->srcBig = $srcBig;
$this->title = $title;
$this->createdAt = $createdAt;
}
public function getId(){
return $this->id;
}
public function setId($id){
$this->id = $id;
}
public function getSrc(){
return $this->src;
}
public function setSrc($src){
$this->src = $src;
}
public function getSrcBig(){
return $this->srcBig;
}
public function setSrcBig($srcBig){
$this->srcBig = $srcBig;
}
public function getTitle(){
return $this->title;
}
public function setTitle($title){
$this->title = $title;
}
public function getCreatedAt(){
return $this->createdAt;
}
public function setCreatedAt($createdAt){
$this->createdAt = $createdAt;
}
}
class Album {
private $id;
private $thumbId;
private $title;
private $description;
private $createdAt;
private $updatedAt;
private $size;
private $photoList;
public function __construct($id = -1, $thumbId = -1, $title = 'unknown', $description = 'unknown', $createdAt = -1, $updatedAt = -1, $size = 0, $photoList = array()){
$this->id = $id;
$this->thumbId = $thumbId;
$this->title = $title;
$this->description = $description;
$this->createdAt = $createdAt;
$this->updatedAt;
$this->size = $size;
$this->photoList = $photoList;
}
public function getId(){
return $this->id;
}
public function setId($id){
$this->id = $id;
}
public function getThumbId(){
return $this->thumbId;
}
public function setThumbId($thumbId){
$this->thumbId = $thumbId;
}
public function getTitle(){
return $this->title;
}
public function setTitle($title){
$this->title = $title;
}
public function getDescription(){
return $this->description;
}
public function setDescription($description){
$this->description = $description;
}
public function getCreatedAt(){
return $this->createdAt;
}
public function setCreatedAt($createdAt){
$this->createdAt = $createdAt;
}
public function getUpdatedAt(){
return $this->updatedAt;
}
public function setUpdatedAt($updatedAt){
$this->updatedAt = $updatedAt;
}
public function getSize(){
return $this->size;
}
public function setSize($size){
$this->size = $size;
}
public function getPhotoList(){
return $this->photoList;
}
public function setPhotoList($photoList){
$this->photoList = $photoList;
}
}
After this manipulations, I needed some converter from vk-api response to my OOP-model. And the code below is for this manipulations:
class Converter {
public static function convertToAlbum($response){
try{
$album = new Album();
$album->setId($response['aid']);
$album->setThumbId($response['thumb_id']);
$album->setTitle($response['title']);
$album->setDescription($response['description']);
$album->setCreatedAt($response['created']);
$album->setUpdatedAt($response['updated']);
$album->setSize($response['size']);
return $album;
} catch(Exception $ex) {
throw new $ex;
}
}
public static function convertToAlbums($response){
try{
$albums = array();
foreach ($response as $r){
$albums[] = self::convertToAlbum($r);
}
return $albums;
} catch (Exception $ex) {
throw new $ex;
}
}
public static function convertToPhoto($response){
try{
$photo = new Photo();
$photo->setId($response['pid']);
$photo->setSrc($response['src']);
$photo->setSrcBig($response['src_xxbig']);
$photo->setTitle($response['text']);
$photo->setCreatedAt($response['created']);
return $photo;
} catch(Exception $ex) {
throw new $ex;
}
}
public static function convertToPhotos($response){
try{
$photos = array();
foreach ($response as $r){
$photos[] = self::convertToPhoto($r);
}
return $photos;
} catch(Exception $ex) {
throw new $ex;
}
}
}
Okay, we've already done Photo, Album and Converter classes. It's time to use this and find out what it can do for us.
define('GROUP_ID', 'YOUR_GROUP_ID');
define('APP_ID', 'YOUR_APP_ID');
define('APP_SECRET', 'YOUR_APP_SECRET_KEY');
require_once('./libs/VK.php');
require_once('./logic/Album.php');
require_once('./logic/Photo.php');
require_once('./logic/Converter.php');
$vk = new \VK\VK(APP_ID, APP_SECRET);
$model = array();
$albumsResponse = $vk->api('photos.getAlbums', array(
'gid' => GROUP_ID
));
$albums = $albumsResponse['response'];
foreach ($albums as $a){
$album = Converter::convertToAlbum($a);
$photosResponse = $vk->api('photos.get', array(
'gid' => GROUP_ID,
'aid' => $album->getId()
));
$photos = $photosResponse['response'];
$album->setPhotoList(Converter::convertToPhotos($photos));
$model[] = $album;
}
}
And now, in our variable $model
we have full list of albums with photos, titles and other info from your vk.com group.