How to fetch Google+ Post into Website using LARAVEL

G+ / Laravel / Laravel Development

Many times, we need to show some of our Google+ images or albums on our website without any need of passing through the Google authentication mechanism. In such cases, we can use the following method.

The Image must be publicly shared on Google+.

We have to perform following steps to fetch Google+ post:

1)  First of all, we should have a valid User Id and Album Id,

            User Id – Username which is used for our gallery in Google+.

Album Id – Name of the Album which is included in our Google+,

2) Now we have to create one job in which we have many functions to set User Id, Album Id and get Album.

– Create Job as follow,

 class GooglePlusPhotoAlbum {

                protected $userId = false;

                protected $albumId = false;

                protected $imageSchema = 'http://a9.com/-/spec/opensearchrss/1.0/';

                protected $photoSchema = 'http://schemas.google.com/photos/2007';

                protected $photoMediaSchema = 'http://search.yahoo.com/mrss/';

                public function __construct() {

  }

  //set User Id

 public function setUserId($userId = false) {

            if(($userId !== false) && is_string($userId)) {

            $this->userId = $userId;

            }

  }

  //set album id

  public function setAlbumId($albumId = false) {

            if(($albumId !== false) && is_string($albumId)) {

            $this->albumId = $albumId;

            }

  }

  //validation of user id and album id

  private function validateIdentifiers() {

            if(($this->userId === false) || !preg_match('/^([a-z0-9]+)$/', $this->                                   userId)) {

                        return false;

            }

            if(($this->albumId === false) || !preg_match('/^([0-9]+)$/', $this->            albumId)) {

            return false;

            }

            return true;

  }

  //get album

  public function getAlbum() {

            if(!$this->validateIdentifiers()) {

            $errorMsg = 'Please provide a valid User and Album ID';

            trigger_error($errorMsg, E_USER_ERROR);

            }

//  The album url which generate xml file(shown in Figure 1) that can be converted into  xml //object by xml_load_file is as follows:

            $feedUrl = sprintf('http://picasaweb.google.com/data/feed/api/user/%                             s/albumid/%s?kind=photo&access=public', $this->userId, $this->    albumId);

            $sxml = simplexml_load_file($feedUrl);

  // get image count 

            $imageCount = $sxml->children($this->imageSchema);

  //get album content from xml object

            $album = array('title'     => (string) $sxml->title,

            'thumbnail' => (string) $sxml->icon,

            'images'    => array(

            'total' => (string) $imageCount->totalResults,

            'media' => array()));

            foreach($sxml->entry as $entry) {

            $photoData  = $entry->children($this->photoSchema);

            $photoMedia = $entry->children($this->photoMediaSchema);

            $thumbnail  = $photoMedia->group->thumbnail[1]->attributes()->{'url'};

            $thumbnails = $this->getMediaUrls($thumbnail);

            $album['images']['media'][] = array(

            'title'        => (string) $entry->title,

            'summary'      => (string) $entry->summary,

            'description'  => (string) $entry->description,

            'commentCount' => (string) $photoData->commentCount,

            'width'        => (string) $photoData->width,

            'height'       => (string) $photoData->height,

            'size'         => (string) $photoData->size,

            'published'    => (string) $photoData->timestamp,

            'thumbnails'   => $thumbnails);

    }

    return $album;

  }

  private function getMediaUrls($url = false) {

  $thumbnails = array(

  'origin' => (string) $url

  );

  //below are the thumbnail sizes , we can set it by our own size ex.'s-700-c' etc

  $mediaSizes = array('s200-c', 's400-c', 'w200', 'w400');

  $subject = $url;

  $pattern = '/^(.*)(\/s144\/)(.*)/';

  preg_match($pattern, $subject, $matches);

   //get thumbnils

  if(isset($matches) && (count($matches) === 4)) {

            foreach($mediaSizes as $media) {

            $thumbnails[$media] = $matches[1] . '/' . $media . '/' . $matches[3];

            }

    }

    return $thumbnails;

  }

}

When we follow the above-mentioned URL with valid User Id and Album id, you will get the XML file as shown in Figure 1.

3) Now we can fetch post from Google+ in our blade file by using above job’s setUserId and setAlbumId function and you can get the album by getAlbum function.

You need to create an object to access the above function,

//create an object to use above function

$googlePlusPhotoAlbum = new App\Jobs\GooglePlusPhotoAlbum();

$googlePlusPhotoAlbum->setUserId($google_plus_user_id);    

$googlePlusPhotoAlbum->setAlbumId($google_plus_album_id);                                          

$album = $googlePlusPhotoAlbum->getAlbum();

Now you have the album and you can get images from the album,

@foreach($album['images']['media'] as $image)

        <img  src="<?= $image['thumbnails']['s720']?>" alt="">

@endforeach

By using this code we can get the post from Google+ on our website.

Figure 2 shows the list of images fetched from Google+.

error:
×