2

I have

$client = new Google_Client();

And it's token in json.

Now I want to pass this client object as well as json token to another script via shell_exec().
Let's assume command as

php myscript.php var1 var2 $client $token

Now as command line takes all argument as string I am not able to pass the json and client object. For json I found serialize() and unserialize() functions that I can pass to command prompt but what about $client object how to pass it to command prompt? Please Help.

Vivek Muthal
  • 995
  • 1
  • 10
  • 23
  • Is there a particular reason you want to `shell_exec()` a php script from another php script ? It would be much simpler if you include it, this sounds a bad idea. – IcanDivideBy0 Sep 26 '13 at 09:47
  • I have a long polling script and so that want to run it from shell it will be in background. But need to pass the client,Now I am thinking rather than passing all this why not re-authenticate $_GET['code'] returned from google. :) – Vivek Muthal Sep 26 '13 at 09:58
  • but problem is that there it is giving error 'invalid grant' as code is already authenticated by front end code. – Vivek Muthal Sep 26 '13 at 10:02
  • I think the issue is you're wanting to send an address that is referencing `new Google_Client()`. I'm not sure if that's possible, although it may be. – JVE999 May 31 '14 at 06:50

3 Answers3

6

Serialize will also "stringify" objects! You can also base64 encode/decode your arguments to prevent special chars troubles :

$aArgs = array($client, $token);
$sArgs = base64_encode(serialize($aArgs));
exec('php myscript.php '.$sArgs);
Nassim
  • 240
  • 1
  • 4
  • Use [`escapeshellarg`](http://www.php.net/manual/fr/function.escapeshellarg.php), not `base64_encode`. – IcanDivideBy0 Sep 26 '13 at 09:48
  • 4
    Yeah sure, but escapeshellarg may have troubles with serialized data => http://www.php.net/manual/en/function.escapeshellarg.php#109514 – Nassim Sep 26 '13 at 10:38
3

I'd use json_encode():

Preferred method to store PHP arrays (json_encode vs serialize)

TLDR? There are some possible issues with json_encode():

  • By default, json_encode() converts UTF-8 characters to Unicode escape sequences while serialize() does not. Note: To leave UTF-8 characters untouched, you can use the option JSON_UNESCAPED_UNICODE as of PHP 5.4.
  • JSON will have no memory of what the object's original class was (they are always restored as instances of stdClass).
  • You can't leverage __sleep() and __wakeup() with JSON
  • Only public properties are serialized with JSON
  • JSON is more portable

But if none of these things are an issue for your use case. it's 100-150% faster than serialize(). (Your Google_Client() class will be converted to a standard class when you decode the string).

// Script that kicks off the background task
$aArgs = array($client, $token);
$sArgs = base64_encode(json_encode($aArgs));
exec('php myscript.php '.$sArgs . ' > /dev/null 2>/dev/null &');

// myscript.php
$sArgs = json_decode(base64_decode($argv[1]));
// do something with $sArgs here...
Community
  • 1
  • 1
Silas Palmer
  • 2,687
  • 1
  • 29
  • 30
0

This is kind of tricky.

First and foremost, if you want to pass exactly the same object to another script, why dont you include it? Or create some function to run instead?

Second, why don't you create your object in that script? It would save you a lot of trouble.

If you absolutely have to pass the object to another script via shell_exec, you may use the serialize() function, but there are a few caveats:

  1. Of course you have to escape it, e.g. with escapeshellarg
  2. If your object contains any resources (such as fopen handle, directly or indirectly), they will not be serialized and thus you will lose them
  3. Your script must be aware of yout Google_Client class definition, and definition of any class that it contains reference to. Otherwise it will be unserialized as __PHP_Incomplete_Class and effectively unusable.

check the manual page serialize

serialize() handles all types, except the resource-type. You can even serialize() arrays that contain references to itself. Circular references inside the array/object you are serializing will also be stored. Any other reference will be lost. When serializing objects, PHP will attempt to call the member function __sleep() prior to serialization. This is to allow the object to do any last minute clean-up, etc. prior to being serialized. Likewise, when the object is restored using unserialize() the __wakeup() member function is called.

and the one for unserialize

Note: unserialize_callback_func directive It's possible to set a callback-function which will be called, if an undefined class should be instantiated during unserializing. (to prevent getting an incomplete object "__PHP_Incomplete_Class".) Use your php.ini, ini_set() or .htaccess to define 'unserialize_callback_func'. Everytime an undefined class should be instantiated, it'll be called. To disable this feature just empty this setting.

Adam Zielinski
  • 2,774
  • 1
  • 25
  • 36
  • it' did not solved my problem, facing some other issue of google api service account, I was already knowing serialize and unserialize but thanks for sharing escapeshellarg. :) – Vivek Muthal Sep 26 '13 at 10:37
  • sure! If you think it answers the original question, do not hesitate to mark it as an accepted answer; If you have some issues with google api, I think you should open another question. – Adam Zielinski Sep 26 '13 at 10:39