How to get the user 'contactid' using google API using PHP - php

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

Related

JSON Decode save array

I am able to receive the following JSON data with the following PHP code:
$json = file_get_contents('https://xxxx');
$data = json_decode($json,true);
$forex = $data['items'];
echo "<pre>";
print_r($forex);
exit;
This gives me the following JSON data:
Array
(
[0] => Array
(
[new] =>
[data] => Array
(
[direction] => 1
[pip] => 0
[exchange] => FOREX
[symbol] => USDCHF
[interval] => 15
[pattern] => Resistance
[complete] =>
[identified] => 2020-02-26T14:45:00.000Z
[age] => 0
[length] => 259
[found] => 2020-02-26T14:45:56.381Z
[result_type] => KeyLevel
[result_uid] => 642525551
[prediction_price_from] => 0
[prediction_price_to] => 0
[group_name] => FX Majors
[symbol_name] => USDCHF
[symbol_id] => 0
[analysis_text] => Approaching Resistance level of 0.9800 identified at 2/26 14:45. This pattern is still in the process of forming. Possible bullish price movement towards the resistance 0.9800 within the next 15 hours.
[expires_at] => 2020-02-27T05:48:36.701Z
)
[links] => Array
(
[0] => Array
(
[rel] => detail
[href] => https://xxxx
)
[1] => Array
(
[rel] => analysis
[href] => https://xxxx
)
[2] => Array
(
[rel] => chart-xs
[href] => https://xxxx
)
[3] => Array
(
[rel] => chart-sm
[href] => https://xxxx
)
[4] => Array
(
[rel] => chart-md
[href] => https://xxxx
)
[5] => Array
(
[rel] => chart-lg
[href] => https://xxxx
)
[6] => Array
(
[rel] => icon-arrow
[href] => https://xxxx
)
)
)
What I am trying to do is to save the information into my MySQL database, however I am unable to get the data displayed even by testing to echo the data:
foreach($forex as $info) {
echo $info->symbol . '<br>';
}
Anyone with a possible solution for me to save the data or even enable me to display the data for each Array (Example: symbol, pattern, link chart-md).
Below the commented code:
//Save json text into variable $json
$json = file_get_contents('https://xxxx');
//Convert json into PHP array
$data = json_decode($json,true);
//Set $forex as $data['items'];
$forex = $data['items'];
//Print
echo "<pre>";
print_r($forex);
exit;
$forex is a PHP array variable (not a JSON string).
You could save your data into MySQL VARCHAR[Length] field using json string variable $json instead of PHP array $forex.
When you need to get the data from DB and display it, you could get JSON data from MySQL and convert it from JSON to Array using PHP json_decode($json,true).
If you want to use a value into a PHP array, use $info["symbol"] instead of $info->symbol

get array name from multilevel array in php

how i can get WP_Widget_Archives from array,
This is my array:
$control = Array
(
[name] => Archives
[id] => archives-6
[callback] => Array
(
[0] => WP_Widget_Archives Object
(
[id_base] => archives
[name] => Archives
[widget_options] => Array
(
[classname] => widget_archive
[description] => A monthly archive of your site’s Posts.
)
[control_options] => Array
(
[id_base] => archives
)
[number] => 8
[id] => archives-8
[updated] =>
[option_name] => widget_archives
)
[1] => form_callback
)
[params] => Array
(
[0] => Array
(
[number] => 6
)
)
[width] => 250
[height] => 200
[id_base] => archives
)
i have try with this code
`echo '<pre>'; print_r(array_keys($control['callback'])); echo '</pre>';`
but I get result like this
Array
(
[0] => 0
[1] => 1
)
where I think the result will be like this
$result = Array
(
[0] => WP_Widget_Archives Object
[1] => form_callback
)
so i can write $result[0] for get WP_Widget_Archives, please help me and thank you for your help :)
Probably you misunderstood array_key function. It will give you keys of the array not value. In your case you require value which is an object 'WP_Widget_Archives', so you can directly use $control['callback'][0].

How to get value out of this...?

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);

HubSpot api json decode

I am trying to parse some data out of the Hubspot API response. The response looks like this json_decoded:
stdClass Object(
[addedAt] => 1411052909604
[vid] => 24
[canonical-vid] => 24
[merged-vids] => Array
(
)
[portal-id] => XXXXX
[is-contact] => 1
[profile-token] => AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[profile-url] => https://app.hubspot.com/contacts/XXXXX/lists/public/contact/_AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[properties] => stdClass Object
(
[lastname] => stdClass Object
(
[value] => testtt
)
[firstname] => stdClass Object
(
[value] => test
)
[lastmodifieddate] => stdClass Object
(
[value] => 1411052906670
)
)
[form-submissions] => Array
(
[0] => stdClass Object
(
[conversion-id] => 85d24dd2-9ee9-4d47-b8f3-3035acbd8f3b
[timestamp] => 1411052834097
[form-id] => fb16efd9-23cc-4511-889c-204fc8b41dba
[portal-id] => 401824
[page-url] => http://wbl-1.hs-sites.com/test
[canonical-url] => http://wbl-1.hs-sites.com/test
[content-type] => landing-page
[page-title] => test
[page-id] => 1570433242
[title] => Default Form (Sample)
[first-visit-url] => http://wbl-1.hs-sites.com/test
[first-visit-timestamp] => 1411052722970
[meta-data] => Array
(
)
)
)
[list-memberships] => Array
(
)
[identity-profiles] => Array
(
[0] => stdClass Object
(
[vid] => 24
[identities] => Array
(
[0] => stdClass Object
(
[type] => EMAIL
[value] => test#user.com
[timestamp] => 1411052834097
)
[1] => stdClass Object
(
[type] => LEAD_GUID
[value] => 0b6acf21-6cee-4c7b-b664-e65c11ee2d8e
[timestamp] => 1411052834201
)
)
)
)
[merge-audits] => Array
(
)
)
I'm looking specifically to try to dig out an email inside the indentities-profile area.
I've tried to do the following:
echo $results->contacts[0]->identity-profiles;
But it just gives me a value of 0
Then I try to go further into the array by doing:
echo $results->contacts[0]->identity-profiles[0];
But at that point - I get a parse error:
Parse error: syntax error, unexpected '['
What am I doing wrong? And how can I dig all the way down to
identity-profiles[0]->identities->[0]->value
which should equal: test#user.com
What am I missing?
As mentioned in the comment I would suggest to decode the JSON to an associative array by passing true as second parameter to json_decode. Example: json_decode($data, true) Than you could access your identity profiles by:
$results['contacts'][0]['identitiy-profiles']
If you still want to get the results as an object you have to access the properties the following way because they contain a -:
$results->contacts[0]->{'identity-profiles'}

Server based Google Drive Service?

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.

Categories