A tarefa era obter todas as fotos dos álbuns do grupo.
Antes de mais nada, precisava da classe PHP VKApi. Você pode encontrá-lo aqui: <a href=” https://github.com/vladkens/VK “target=” blank”> vk api </a>.
A segunda coisa que fiz foi ler os documentos da API vk.com <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% B2 API “alvo =” em branco “> aqui </a>. Eu preciso da seção de fotos deste documento.
Para construir um aplicativo vk com essa classe php vk-api, você precisa:
define('APP_ID', 'YOUR_APP_ID');
define('APP_SECRET', 'YOUR_APP_SECRET_KEY');
require_once('./libs/VK.php');
$vk = new VKVK(APP_ID, APP_SECRET);
O próximo que eu queria é fazer funcionar no estilo OOP. Criei duas classes chamadas Foto e Álbum. Aqui estão eles:
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;
}
}
Após essas manipulações, eu precisava de algum conversor de resposta vk-api para meu modelo OOP. E o código abaixo é para essas manipulações:
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;
}
}
}
Ok, já fizemos aulas de Foto, Álbum e Conversor. É hora de usar isso e descobrir o que isso pode fazer por nós.
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 VKVK(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;
}
}
E agora, em nossa variável $model
, temos lista completa de álbuns com fotos, títulos e outras informações do seu grupo vk.com.