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.
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);
I'd use json_encode():
Preferred method to store PHP arrays (json_encode vs serialize)
TLDR? There are some possible issues with json_encode():
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With