Skip to content

Instantly share code, notes, and snippets.

@thewheat
Created November 26, 2016 08:29
Show Gist options
  • Save thewheat/11448eefffe701aab5d6ace8c7f98836 to your computer and use it in GitHub Desktop.
Save thewheat/11448eefffe701aab5d6ace8c7f98836 to your computer and use it in GitHub Desktop.
Upload photos / interacting with Picasa Web Albums / Google Photos via old API https://developers.google.com/picasa-web/docs/2.0/developers_guide_protocol
{
"name": "thewheat/code",
"authors": [
{
"name": "Timothy Lim",
"email": "tim.lim@intercom.io"
}
],
"require": {
"google/apiclient": "^2.0"
}
}
<?php
/*
- Interacting with Picasa Web Albums / Google Photos via old API https://developers.google.com/picasa-web/docs/2.0/developers_guide_protocol
- Install depdencies via composer https://getcomposer.org/
- Get dependencies with `composer install`
- Minimal composer.json file below
===============================================================================
composer.json
===============================================================================
{
"name": "thewheat/code",
"authors": [
{
"name": "Timothy Lim",
"email": "tim.lim@intercom.io"
}
],
"require": {
"google/apiclient": "^2.0"
}
}
===============================================================================
- Getting a service account key from https://console.developers.google.com/
- Specify $user_to_impersonate value to indicate user to upload as
*/
// include your composer dependencies
require_once 'vendor/autoload.php';
use GuzzleHttp\Psr7\Request;
// Get a service account key from https://console.developers.google.com/
putenv('GOOGLE_APPLICATION_CREDENTIALS=./auth.json');
$user_to_impersonate = "EMAIL@DOMAIN.COM"; // email address of the user to upload data with
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject($user_to_impersonate);
$client->addScope('https://picasaweb.google.com/data/');
// returns a Guzzle HTTP Client
$httpClient = $client->authorize();
// make an HTTP request
function debugResponse($response){
print_r($response);
echo "\ngetStatusCode: " . $response->getStatusCode(); // >>> 200
echo "\ngetReasonPhrase: " . $response->getReasonPhrase(); // >>> OK
echo "\nbody: " . $response->getBody();
echo "\n";
}
function listAlbums($httpClient){
echo "=======================================\n";
echo "Listing albums\n";
echo "---------------------------------------\n";
$response = $httpClient->get('https://picasaweb.google.com/data/feed/api/user/default');
$xml=simplexml_load_string($response->getBody());
for($i = 0; $i < count($xml->entry); $i++){
echo "$i. id: " . $xml->entry[$i]->id . "\n";
echo "$i. title: " . $xml->entry[$i]->title . "\n";
}
echo "=======================================\n";
}
function createAlbum($httpClient, $name){
echo "=======================================\n";
echo "Creating album\n";
echo "---------------------------------------\n";
$xmlData = "" .
"<entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gphoto='http://schemas.google.com/photos/2007'>
<title type='text'>1234123</title>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/photos/2007#album'></category>
</entry>";
$xml=simplexml_load_string($xmlData);
$xml->title = $name;
$response = $httpClient->post('https://picasaweb.google.com/data/feed/api/user/default', [
'headers' => ['Content-Type' => 'application/atom+xml'],
'body' => $xml->asXML()
]);
$xml_response=simplexml_load_string($response->getBody());
echo "id: " . $xml_response->id . "\n";
echo "title: " . $xml_response->title . "\n";
echo "=======================================\n";
}
function listAlbum($httpClient, $id){
$response = $httpClient->get("https://picasaweb.google.com/data/feed/api/user/default/albumid/" . $id);
$xml=simplexml_load_string($response->getBody());
echo "=======================================\n";
echo "Listing photos in album\n";
echo "---------------------------------------\n";
echo "Album id: " . $xml->id . "\n";
echo "Album title: " . $xml->title . "\n";
echo "---------------------------------------\n";
for($i = 0; $i < count($xml->entry); $i++){
echo "$i. id: " . $xml->entry[$i]->id . "\n";
echo "$i. title: " . $xml->entry[$i]->title . "\n";
}
echo "=======================================\n";
}
function uploadPhoto($httpClient, $id, $image_path, $title = ""){
echo "=======================================\n";
echo "Upload photo to album\n";
echo "---------------------------------------\n";
echo "Album ID: " . $id . "\n";
echo "Image Path: " . $image_path . "\n";
if(file_exists($image_path)){
echo "File exists. Upload it!\n";
$body = fopen($image_path, 'r');
$data = [
'body' => $body
];
if($title != "") {
$data['headers'] = ['Slug' => $title];
}
$response = $httpClient->post("https://picasaweb.google.com/data/feed/api/user/default/albumid/" . $id, $data);
$xml_response=simplexml_load_string($response->getBody());
echo "id: " . $xml_response->id . "\n";
echo "title: " . $xml_response->title . "\n";
}
else{
echo "File doesn't exist\n";
}
echo "=======================================\n";
}
//=============================================================================
// Example usage:
//=============================================================================
//listAlbums($httpClient);
//createAlbum($httpClient, "create album via PHP");
//listAlbum($httpClient, "6352386383096829777");
//uploadPhoto($httpClient, "6352386383096829777", "test.jpg", "title");
//=============================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment