I am looking to build a Google Drive application to store image and document files from a website application, without the requirement of a visitor authenticating, or providing permission to access, etc. Seamless use of a googledrive service through a PHP interface.
Users will upload images/documents to a web form, and the files are stored on a GoogleDrive instead of local file system.
I'm just in need of assistance with this, as all the Google Client examples I've worked with require user based authorization, and permissions.
The code I have now seems to get me my Google_DriveService array:
require_once '../apis/google-api-php-client/src/Google_Client.php';
require_once '../apis/google-api-php-client/src/contrib/Google_PredictionService.php';
require '../apis/google-api-php-client/src/contrib/Google_DriveService.php';
session_start();
// Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
// https://developers.google.com/console/help/#service_accounts
const CLIENT_ID = '########.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = '##########developer.gserviceaccount.com';
// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = '##########-privatekey.p12';
// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$client = new Google_Client();
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/prediction'),$key));
$client->setClientId(CLIENT_ID);
$service = new Google_PredictionService($client);
// Prediction logic:
$id = '9146497114232; # replaced with random numbers for this post
$hostedModelName= 'sample.languageid';
$predictionData = new Google_InputInput();
$predictionData->setCsvInstance(array('Foo Bar A Doo')); ## Not sure what this is or what I need here?
$input = new Google_Input();
$input->setInput($predictionData);
$result = $service->hostedmodels->predict($id,$hostedModelName, $input);
# print '<h2>Prediction Result:</h2><pre>' . print_r($result, true) . '</pre>';
// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
$driveService = new Google_DriveService($client);
print_r($driveService);
exit;
I am returned a lengthy array of objects, and from there, I'm rather stone-walled.
Some of which looks like :
[service:Google_ServiceResource:private] => Google_DriveService Object
*RECURSION*
[serviceName:Google_ServiceResource:private] => drive
[resourceName:Google_ServiceResource:private] => revisions
[methods:Google_ServiceResource:private] => Array
(
[delete] => Array
(
[id] => drive.revisions.delete
[path] => files/{fileId}/revisions/{revisionId}
[httpMethod] => DELETE
[parameters] => Array
(
[fileId] => Array
(
[type] => string
[required] => 1
[location] => path
)
[revisionId] => Array
(
[type] => string
[required] => 1
[location] => path
)
)
[scopes] => Array
(
[0] => https://www.googleapis.com/auth/drive
[1] => https://www.googleapis.com/auth/drive.file
)
)
[get] => Array
(
[id] => drive.revisions.get
[path] => files/{fileId}/revisions/{revisionId}
[httpMethod] => GET
[parameters] => Array
(
[fileId] => Array
(
[type] => string
[required] => 1
[location] => path
)
[revisionId] => Array
(
[type] => string
[required] => 1
[location] => path
)
)
[response] => Array
(
[$ref] => Revision
)
[scopes] => Array
(
[0] => https://www.googleapis.com/auth/drive
[1] => https://www.googleapis.com/auth/drive.file
[2] => https://www.googleapis.com/auth/drive.metadata.readonly
[3] => https://www.googleapis.com/auth/drive.readonly
)
)
[list] => Array
(
[id] => drive.revisions.list
[path] => files/{fileId}/revisions
[httpMethod] => GET
[parameters] => Array
(
[fileId] => Array
(
[type] => string
[required] => 1
[location] => path
)
)
[response] => Array
(
[$ref] => RevisionList
)
[scopes] => Array
(
[0] => https://www.googleapis.com/auth/drive
[1] => https://www.googleapis.com/auth/drive.file
[2] => https://www.googleapis.com/auth/drive.metadata.readonly
[3] => https://www.googleapis.com/auth/drive.readonly
)
)
[patch] => Array
(
[id] => drive.revisions.patch
[path] => files/{fileId}/revisions/{revisionId}
[httpMethod] => PATCH
[parameters] => Array
(
[fileId] => Array
(
[type] => string
[required] => 1
[location] => path
)
[revisionId] => Array
(
[type] => string
[required] => 1
[location] => path
)
)
[request] => Array
(
[$ref] => Revision
)
[response] => Array
(
[$ref] => Revision
)
[scopes] => Array
(
[0] => https://www.googleapis.com/auth/drive
[1] => https://www.googleapis.com/auth/drive.file
)
)
[update] => Array
(
[id] => drive.revisions.update
[path] => files/{fileId}/revisions/{revisionId}
[httpMethod] => PUT
[parameters] => Array
(
[fileId] => Array
(
[type] => string
[required] => 1
[location] => path
)
[revisionId] => Array
(
[type] => string
[required] => 1
[location] => path
)
)
[request] => Array
(
[$ref] => Revision
)
[response] => Array
(
[$ref] => Revision
)
[scopes] => Array
(
[0] => https://www.googleapis.com/auth/drive
[1] => https://www.googleapis.com/auth/drive.file
)
)
)
)
[version] => v2
[servicePath] => drive/v2/
[resource] =>
[serviceName] => drive
I think you should look into using a service acccount for this.
https://developers.google.com/drive/web/service-accounts
By using a service account you wont have to worry about people loging in. Everthing will always be uploaded to the same service account.
Related
I'm working on the PHP script where I want to get and display all message which is received on the plivo number?
I have already tried two different methods which are not working
Please guide how do I get the your_message_uuid which is necessary in Method 1?
Method 1
require 'vendor/autoload.php';
use Plivo\RestClient;
$client = new RestClient("auth id", "auth_token");
$response = $client->messages->get('your_message_uuid');
print_r($response);
Method 2
require 'vendor/autoload.php';
use Plivo\RestClient;
$client = new RestClient("auth id", "auth_token");
$response = $client->messages->list(
[
'limit' => 5,
'offset' => 0,
'subaccount' =>'subaccount_auth_id'
]
);
print_r($response);
// Prints only the message_uuid
print_r($response->getmessageUuid(0));
Result Using Method 1
Plivo\Resources\Message\Message Object ( [client:protected] => Plivo\BaseClient Object ( [timeout:protected] => [httpClientHandler:protected] => Plivo\HttpClients\PlivoGuzzleHttpClient Object ( [guzzleClient:protected] => GuzzleHttp\Client Object ( [config:GuzzleHttp\Client:private] => Array ( [handler] => GuzzleHttp\HandlerStack Object ( [handler:GuzzleHttp\HandlerStack:private] => Closure Object ( [static] => Array ( [default] => GuzzleHttp\Handler\CurlMultiHandler Object ( [factory:GuzzleHttp\Handler\CurlMultiHandler:private] => GuzzleHttp\Handler\CurlFactory Object ( [handles:GuzzleHttp\Handler\CurlFactory:private] => Array ( ) [maxHandles:GuzzleHttp\Handler\CurlFactory:private] => 50 ) [selectTimeout:GuzzleHttp\Handler\CurlMultiHandler:private] => 1 [active:GuzzleHttp\Handler\CurlMultiHandler:private] => [handles:GuzzleHttp\Handler\CurlMultiHandler:private] => Array ( ) [delays:GuzzleHttp\Handler\CurlMultiHandler:private] => Array ( ) [options:GuzzleHttp\Handler\CurlMultiHandler:private] => Array ( ) ) [sync] => GuzzleHttp\Handler\CurlHandler Object ( [factory:GuzzleHttp\Handler\CurlHandler:private] => GuzzleHttp\Handler\CurlFactory Object ( [handles:GuzzleHttp\Handler\CurlFactory:private] => Array ( [0] => Resource id #55 ) [maxHandles:GuzzleHttp\Handler\CurlFactory:private] => 3 ) ) ) [parameter] => Array ( [$request] => [$options] => ) ) [stack:GuzzleHttp\HandlerStack:private] => Array ( [0] => Array ( [0] => Closure Object ( [parameter] => Array ( [$handler] => ) ) [1] => http_errors ) [1] => Array ( [0] => Closure Object ( [parameter] => Array ( [$handler] => ) ) [1] => allow_redirects ) [2] => Array ( [0] => Closure Object ( [parameter] => Array ( [$handler] => ) ) [1] => cookies ) [3] => Array ( [0] => Closure Object ( [parameter] => Array ( [$handler] => ) ) [1] => prepare_body ) ) [cached:GuzzleHttp\HandlerStack:private] => Closure Object ( [static] => Array ( [handler] => GuzzleHttp\RedirectMiddleware Object ( [nextHandler:GuzzleHttp\RedirectMiddleware:private] => Closure Object ( [static] => Array ( [handler] => GuzzleHttp\PrepareBodyMiddleware Object ( [nextHandler:GuzzleHttp\PrepareBodyMiddleware:private] => Closure Object ( [static] => Array ( [default] => GuzzleHttp\Handler\CurlMultiHandler Object ( [factory:GuzzleHttp\Handler\CurlMultiHandler:private] => GuzzleHttp\Handler\CurlFactory Object ( [handles:GuzzleHttp\Handler\CurlFactory:private] => Array ( ) [maxHandles:GuzzleHttp\Handler\CurlFactory:private] => 50 ) [selectTimeout:GuzzleHttp\Handler\CurlMultiHandler:private] => 1 [active:GuzzleHttp\Handler\CurlMultiHandler:private] => [handles:GuzzleHttp\Handler\CurlMultiHandler:private] => Array ( ) [delays:GuzzleHttp\Handler\CurlMultiHandler:private] => Array ( ) [options:GuzzleHttp\Handler\CurlMultiHandler:private] => Array ( ) ) [sync] => GuzzleHttp\Handler\CurlHandler Object ( [factory:GuzzleHttp\Handler\CurlHandler:private] => GuzzleHttp\Handler\CurlFactory Object ( [handles:GuzzleHttp\Handler\CurlFactory:private] => Array ( [0] => Resource id #55 ) [maxHandles:GuzzleHttp\Handler\CurlFactory:private] => 3 ) ) ) [parameter] => Array ( [$request] => [$options] => ) ) ) ) [parameter] => Array ( [$request] => [$options] => ) ) ) ) [parameter] => Array ( [$request] => [$options] => ) ) ) [allow_redirects] => Array ( [max] => 5 [protocols] => Array ( [0] => http [1] => https ) [strict] => [referer] => [track_redirects] => ) [http_errors] => 1 [decode_content] => 1 [verify] => 1 [cookies] => [idn_conversion] => 1 [headers] => Array ( [User-Agent] => GuzzleHttp/6.5.5 curl/7.75.0 PHP/7.2.34 ) ) ) [authId:protected] => [authToken:protected] => ) [basicAuth:protected] => Plivo\Authentication\BasicAuth Object ( [authId:protected] => [authToken:protected] => ) ) [interface:protected] => [id:protected] => [pathParams:protected] => Array ( [authId] => [messageUuid] => ) [properties] => Array ( [from] => [to] => [messageDirection] => [messageState] => [messageTime] => [messageType] => [messageUuid] => [resourceUri] => [totalAmount] => [totalRate] => [units] => ) )
Empty Result Using Method 2
Check out Plivo's use case guide to receiving SMS messages. It explains how to
create a PHP app to read the message content
create a Plivo SMS app and assign the PHP app to it
assign the Plivo SMS app to a Plivo number (that will receive the
incoming SMS messages)
whenever an incoming SMS message is received on that Plivo number, Plivo
will send the details to his app server configured in the Plivo SMS
app (message URL)
Hello folks I'm trying to access the created_on object using code below but it is not working, I'm getting incorrect date January 1, 1970 12:00. Also tried
$item = PodioItem::get($item_id);
$file_id = "";
$created_on = "";
foreach ($item->files as $file) {
echo date('F j, Y h:i',strtotime($file->created_on))."<br>";
}
Below is the response object from print_r($file,true), I can access the other object without any issue like $file->file_id, $file->link. What I'm trying to accomplish is store the created_on data in a variable then create a function to check and grab the latest date, then grab the file_id of it.
PodioFile Object
[__attributes:PodioObject:private] => Array
(
[file_id] => 464714088
[link] => https://files.podio.com/464714088
[perma_link] =>
[thumbnail_link] =>
[hosted_by] => podio
[name] => KEjj4UQSUV.PDF
[description] =>
[mimetype] => application/pdf
[size] => 785849
[created_on] => 2017-12-08 23:03:36
[rights] => Array
(
[0] => download
[1] => view
[2] => delete
[3] => update
)
PodioFile Object
(
[__attributes:PodioObject:private] => Array
(
[file_id] => 464716575
[link] => https://files.podio.com/464716575
[perma_link] =>
[thumbnail_link] =>
[hosted_by] => podio
[name] => IiaCfJjwws.PDF
[description] =>
[mimetype] => application/pdf
[size] => 785849
[created_on] => 2017-12-08 23:15:30
[rights] => Array
(
[0] => download
[1] => view
[2] => delete
[3] => update
)
PodioFile Object
(
[__attributes:PodioObject:private] => Array
(
[file_id] => 464728331
[link] => https://files.podio.com/464728331
[perma_link] =>
[thumbnail_link] =>
[hosted_by] => podio
[name] => g6cmAeUCiV.PDF
[description] =>
[mimetype] => application/pdf
[size] => 785849
[created_on] => 2017-12-08 23:49:15
[rights] => Array
(
[0] => download
[1] => view
[2] => delete
[3] => update
)
Luckily found the answer on sample provided here - https://hotexamples.com/examples/-/PodioItem/filter/php-podioitem-filter-method-examples.html
date_format($file->created_on, 'Y/m/d H:i:s') did the trick
I need somehow to get the list of existing permissions in my Yii2 application.
yii/rbac/DbManager as authManager is used.
And when trying to run:
$permissions = Yii::$app->authManager->getPermissions();
I receive list in which are permissions and routes:
Array ( [/admin-tools/*] => yii\rbac\Permission Object ( [type] => 2 [name] => /admin-tools/* [description] => Access admin-tools [ruleName] => [data] => [createdAt] => 1500542371 [updatedAt] => 1500542371 ) [/admin/*] => yii\rbac\Permission Object ( [type] => 2 [name] => /admin/* [description] => access admin extension [ruleName] => [data] => [createdAt] => 1500541632 [updatedAt] => 1500541632 ) [/group] => yii\rbac\Permission Object ( [type] => 2 [name] => /group [description] => [ruleName] => [data] => [createdAt] => 1502190828 [updatedAt] => 1502190828 ) ) [permission_admin] => yii\rbac\Permission Object ( [type] => 2 [name] => permission_admin [description] => [ruleName] => [data] => [createdAt] => 1500559417 [updatedAt] => 1502190927 ) [permission_hr] => yii\rbac\Permission Object ( [type] => 2 [name] => permission_hr [description] => [ruleName] => [data] => [createdAt] => 1500559479 [updatedAt] => 1500559919 ) )
I suppose there is another smarter way to get this data. Because yii2-admin module is able to get list of only permissions, but I don`t have any idea how to do this?.
The resulting
$permissions = Yii::$app->authManager->getPermissions();
is an array with all the permissions object content
if you need only name and description you can iterate over the array eg:
$permissions = Yii::$app->authManager->getPermissions();
foreach($permissions as $key => $perm){
echo 'name : ' . $perm->name . ' description : ' . $perm->description . <br />;
echo
}
you can check yii2 doc http://www.yiiframework.com/doc-2.0/yii-rbac-dbmanager.html
http://www.yiiframework.com/doc-2.0/yii-rbac-basemanager.html#getPermissions()-detail
could b you are looking for getPermissionsByUser()
http://www.yiiframework.com/doc-2.0/yii-rbac-dbmanager.html#getPermissionsByUser()-detail
Can someone explain me how to get data out of this...like if I just want subject, description..etc...
stdClass Object
(
[tickets] => Array
(
[0] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/1.json
[id] => 1
[external_id] =>
[via] => stdClass Object
(
[channel] => sample_ticket
[source] => stdClass Object
(
[from] => stdClass Object
(
)
[to] => stdClass Object
(
)
[rel] =>
)
)
[created_at] => 2015-04-22T08:30:29Z
[updated_at] => 2015-05-19T06:01:22Z
[type] => incident
[subject] => This is a sample ticket requested and submitted by you
[raw_subject] => This is a sample ticket requested and submitted by you
[description] => This is the first comment. Feel free to delete this sample ticket.
[priority] => high
[status] => closed
[recipient] =>
[requester_id] => 794599791
[submitter_id] => 794599791
[assignee_id] => 794599791
[organization_id] => 39742491
[group_id] => 24344491
[collaborator_ids] => Array
(
)
[forum_topic_id] =>
[problem_id] =>
[has_incidents] =>
[due_at] =>
[tags] => Array
(
[0] => sample
[1] => zendesk
)
[custom_fields] => Array
(
)
[satisfaction_rating] =>
[sharing_agreement_ids] => Array
(
)
[fields] => Array
(
)
[followup_ids] => Array
(
)
[brand_id] => 565681
)
[1] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/10.json
[id] => 10 //multiple object like [0]...
Thanks...Any help would be great..
When you need to access to array's key, use []. When you have object, use ->.
echo $obj->tickets[0]->subject; // returns first subject
echo $obj->tickets[0]->description; // returns first description
You can put it into foreach loop, of course to gain all subjects, etc.
It's STD object so use properties
$obj->tickets[0]->subject
$obj->tickets[0]->description
You can obviously loop tickets
foreach($obj->tickets as $ticket)
{
echo $ticket->subject;
echo $ticket->description
}
this is an std object.to get subject and description follow this
$obj->tickets[0]->subject;
$obj->tickets[0]->description;
if you feel better in array just make it array using this code
$array = get_object_vars($obj);
I am trying to fetch user contacts with all user details uaing the google API 3.0.
I am able to get the JSON response with the details of the user.
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
The result is
[entry] => Array
(
[0] => Array
(
[id] => Array
(
[$t] => http://www.google.com/m8/feeds/contacts/sfdhitdf1%40gmail.com/base/1
)
[gd$etag] => "RXc7fTVSLit7I2A9XRZaEkoLRAw."
[updated] => Array
(
[$t] => 2014-08-29T00:16:24.905Z
)
[app$edited] => Array
(
[xmlns$app] => http://www.w3.org/2007/app
[$t] => 2014-08-29T00:16:24.905Z
)
[category] => Array
(
[0] => Array
(
[scheme] => http://schemas.google.com/g/2005#kind
[term] => http://schemas.google.com/contact/2008#contact
)
)
[title] => Array
(
[$t] => abc
)
[link] => Array
(
[0] => Array
(
[rel] => http://schemas.google.com/contacts/2008/rel#photo
[type] => image
[href] => https://www.google.com/m8/feeds/photos/media/sachdfwdfd%40gmail.com/1?v=3.0
[gd$etag] => "VWVIH3oyWit7I2B0UBRURzwNBWM8ODs8cSk."
)
[1] => Array
(
[rel] => self
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/sachitdff%40gmail.com/full/1?v=3.0
)
[2] => Array
(
[rel] => edit
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/sachidtfd%40gmail.com/full/1?v=3.0
)
)
[gd$name] => Array
(
[gd$fullName] => Array
(
[$t] => abc xyz
)
[gd$givenName] => Array
(
[$t] => abc
)
[gd$familyName] => Array
(
[$t] => xyz
)
)
[gd$email] => Array
(
[0] => Array
(
[address] => abi9#gmail.com
[primary] => true
[rel] => http://schemas.google.com/g/2005#other
)
)
[gContact$website] => Array
(
[0] => Array
(
[href] => http://www.google.com/profiles/104048264070958665151
[rel] => profile
)
)
[gContact$groupMembershipInfo] => Array
(
[0] => Array
(
[deleted] => false
[href] => http://www.google.com/m8/feeds/groups/sachitaware
)
)
But here I dont get the contact image of the user.The documentation says I need a contact id for getting the photo,but I dont get a contact id in the response above.How can I get the contact id of the user and subsequently his contact photo?
I have authorized the app using oauth 2.0 and apart from image I get most of the details of the contact.
EDIT: I tried this way from the documentation and it works,but it returns the binary image instead of the image URL and I have to send another request to get the image.
$url1 ='https://www.google.com/m8/feeds/photos/media/{useremail}/13444? v=3.0&oauth_token='.$accesstoken;
$xmlresponse1 = curl($url1);
To display the image:
<img src="data:image/*;base64,<?php echo base64_encode($xmlresponse1); ?> />
Can't I get the contact image URL something like facebook returns?
According the documentation, contactIid is returned in the contact entry URL returned by the API:
http://www.google.com/m8/feeds/contacts/<userEmail>/base/<contactId>
So, giving your sample:
http://www.google.com/m8/feeds/contacts/sfdhitdf1%40gmail.com/base/1
We have these values:
userEmail: sfdhitdf1#gmail.com
contactId: 1