Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter passing array to controller via CLI

I need to run one of my CI scripts on the command-line. I need to pass an array to the controller to then pass to the script. Here's what I've got right now:

$params = array(
    'a' => var1,
    'b' => var2
);

Then the cmd running is:

php index.php process process_opk params

In my controller, just to see how/if the array is coming through properly I have:

public function index($args) {
    print_r($args);
}

and the output of this is params as a string.

Do I need to serialize my array first before sending it? I guess CLI changes how variables are passed through CLI, am I wrong? If anyone could elaborate on this and demonstrate best practice, that would be great. Thanks!

Update: Best solution I could find so far is to base64_encode the serialized data and send it as a long string. Then in the controller decode and unserialize and send the array to my script.

like image 558
Jared Eitnier Avatar asked Sep 07 '26 03:09

Jared Eitnier


1 Answers

By default CI allows "a-z 0-9~%.:_-" characters. base64 produces another symbols like + and =. That's why it can be better to use rawurlencode instead of base64:

exec( 'php index.php controller function '.rawurlencode(serialize($params)) );

It's safe for transfering & shell.

like image 129
Dmitriy Lapin Avatar answered Sep 10 '26 21:09

Dmitriy Lapin