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

joshnik
  • 438
  • 1
  • 5
  • 15
  • I know "admin scopes" isn't a valid scope, I was just shortening the actual scopes. The client works :) – joshnik Mar 14 '17 at 08: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();`. – Daniel W. Mar 14 '17 at 17:01

1 Answers1

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?

Community
  • 1
  • 1
Noah Cristino
  • 757
  • 8
  • 29