Thứ Năm, 24 tháng 4, 2014

Google Data APIs [Media Sharing]

Google Data APIs provide a simple interface for applications tread and write data intvarious Google services. The Data APIs use a protocol similar to the Atom Publishing Protocol for data transfer. All the services are implemented in the packagcalled 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 allowyou tfetch, edit, and managyour 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 necomment
        getCommentEntry()Fetches the specified comment
        insertTagEntry(): Creates a netag
        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.
Beforgetting started, make suryou have uploaded some photos on your Google Photos account.
Follow these steptfetch 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 cliento 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.       Nofetch 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   );


$albumFeed = $gp->getAlbumFeed($query);

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 trender the albums; this can be placed in the media manager's index viewCommunicationApp/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:


What just happened?
We have successfully used Google Data APIs to fetch Picasa upload information from Google and used that information to render galleries in our application.
Your nextask will be to implement the photo upload option using Google Data APIs when viewing a photo in the photgallery; you will have a button that will allow you to upload the photto Google Photos.

Không có nhận xét nào:

Đăng nhận xét