Google Data APIs provide a simple interface for applications to read and write data into various Google services. The Data APIs use a protocol similar to the Atom Publishing Protocol for data transfer. All the services are implemented in the package called ZendGdata.
Some of the most frequently used Google services that are supported by the ZendGdata API are listed as follows:
◆ Picasa Web Albums
◆ YouTube
◆ Google Calendar
◆ Google Spreadsheets
◆ Google Documents
◆ Google Provisioning
◆ Google Analytics
◆ Google Blogger
◆ Google CodeSearch
◆ Google Notebook
Since ZendGdata is not provided with the default Zend Framework installation, this needs to be installed manually. This can be performed using Composer and by fetching "zendframework/zendgdata": "2.*".
The Google Photos API
The Google Photos API allows you to fetch, edit, and manage your photos and albums in your Picasa or Google+ accounts. The Data API provides all kinds of services; some of the key functions are listed as follows:
◆ getUserFeed(): Gets all the associated albums for that user
◆ insertAlbumEntry(): Creates a new album
◆ getAlbumFeed(): Fetches the specified album
◆ insertPhotoEntry(): Creates a new photo
◆ getPhotoFeed(): Fetches the specified photo
◆ insertCommentEntry(): Creates a new comment
◆ getCommentEntry(): Fetches the specified comment
◆ insertTagEntry(): Creates a new tag
◆ getTagEntry(): Fetches the specified tag
◆ deleteAlbumEntry(): Deletes the album
◆ deletePhotoEntry(): Deletes the photo
◆ deleteCommentEntry(): Deletes the comment
◆ deleteTagEntry(): Deletes the tag
In this example we will fetch the user's existing albums and the photos stored inside those albums.
Before getting started, make sure you have uploaded some photos on your Google Photos account.
Follow these steps to fetch photos from your Google Photos account:
1. Create a method, getGooglePhotos(), in your controller that will connect to Google Photos and fetch all albums from Google Photos. This method needs to be placed in the MediaManagerController file,src/Users/Controller/ MediaManagerController.php.
2. Set up the API client to make use of the Curl request with the option to disable
sslverifypeer.
$adapter = new \Zend\Http\Client\Adapter\Curl();
$adapter->setOptions(array( 'curloptions' => array(
CURLOPT_SSL_VERIFYPEER => false,
)
));
$httpClient = new \ZendGData\HttpClient();
$httpClient->setAdapter($adapter);
$client = \ZendGData\ClientLogin::getHttpClient( self::GOOGLE_USER_ID, self::GOOGLE_PASSWORD,
\ZendGData\Photos::AUTH_SERVICE_NAME,
$httpClient);
3. Now create a new Google Photos client using the API client.
$gp = new \ZendGData\Photos($client);
4. Now fetch the list of albums using getUserFeed() and get the list of images inside the album using getAlbumFeed().
$userFeed = $gp->getUserFeed( self::GOOGLE_USER_ID ); foreach ($userFeed as $userEntry) {
$albumId = $userEntry->getGphotoId()->getText();
$gAlbums[$albumId]['label'] = $userEntry->getTitle()-
>getText();
$query = $gp->newAlbumQuery();
$query->setUser( self::GOOGLE_USER_ID );
$query->setAlbumId( $albumId );
foreach ($albumFeed as $photoEntry) {
$photoId = $photoEntry->getGphotoId()->getText();
if ($photoEntry->getMediaGroup()->getContent() != null) {
$mediaContentArray = $photoEntry->getMediaGroup()-
>getContent();
$photoUrl = $mediaContentArray[0]->getUrl();
}
if ($photoEntry->getMediaGroup()->getThumbnail() != null)
{
$mediaThumbnailArray = $photoEntry->getMediaGroup()-
>getThumbnail();
$thumbUrl = $mediaThumbnailArray[0]->getUrl();
}
$albumPhoto = array();
$albumPhoto['id'] = $photoId;
$albumPhoto['photoUrl'] = $photoUrl;
$albumPhoto['thumbUrl'] = $thumbUrl;
$gAlbums[$albumId]['photos'][] =$albumPhoto;
}
}
// Return the consolidated array back to the view for rendering return $gAlbums;
5. The following code block in the album view is used to render the albums; this can be placed in the media manager's index view, CommunicationApp/module/ Users/view/users/media-manager/index.phtml:
<?php foreach ($googleAlbums as $googleAlbum) : ?>
<h4> <?php echo $this->escapeHtml($googleAlbum['label']);?>
</h4>
<?php foreach ($googleAlbum['photos'] as $googleAlbumPhoto) : ?>
<div class = "googleAlbumPhoto" style="padding:10px; display:inline">
<a href="<?php echo $this->escapeHtml($googleAlbumPhoto['p hotoUrl']);?>">
<img src="<?php echo $this->escapeHtml($googleAlbumPhoto[' thumbUrl']);?>" />
</a>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
<hr />
6. Upload pictures to your Google Photos album:
7. Open the page in a browser window; you should be able to see all available albums and photos inside the album:
Không có nhận xét nào:
Đăng nhận xét