0

Apologies if I am missing something obvious in this question...

I have an index.php file where I initialise a Google Client, set the scopes, create a service and start pulling data from the Google Tenancy. This works fine.

<?php 
require_once 'vendor/autoload.php';

const CLIENT_ID = MY CLIENT ID;
const CLIENT_SECRET = MY SECRET;
const REDIRECT_URI = REDIRECT URI;


session_start();

$client = new Google_Client();
$client->setApplicationName("My Application");
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);
$client->setScopes('admin scopes');


$adminservice = new Google_Service_Directory($client);

My issue is, I want to get a user's ID from the AdminDirectory API and then pass it to a new page, user.php, with the GET tag id.

So, for example:

<?php
$id = $adminservice->users->get(EMAIL)->id;
?>

<a href = 'user.php?id=<?php echo $id; ?>'>Click Here</a>

How do I transfer my $client variable over to this new user.php page?

I have tried putting the client in $_SESSION['client'] and then extracting it on the new page. I have also tried reinitialising the entire client. Neither seem to work.

Thanks

2
  • I know "admin scopes" isn't a valid scope, I was just shortening the actual scopes. The client works :) Commented Mar 14, 2017 at 8:59
  • You can't transfer an instance between page requests because the methods of the class can't get serialized. You need to use the same code, new Google_Client();. Commented Mar 14, 2017 at 17:01

1 Answer 1

0

You have to import the class on the user.php page:

require_once 'vendor/autoload.php';

Next, store the object in the session:

$_SESSION['googleclient'] = $client;

Now to get it on the other page do:

$client = $_SESSION['client'];

If you need anymore help look at this: move object from 1 page to another?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.