Jared Eitnier
Jared Eitnier

Reputation: 7152

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.

Upvotes: 3

Views: 4030

Answers (4)

Kanan Farzali
Kanan Farzali

Reputation: 1053

To pass it into a new thread with post data:

exec('nohup php index.php controller method ' . rawurlencode($this->input->raw_input_stream) . ' ' . arg2 . ' ' . $arg3 . ' > /dev/null 2>&1 & echo $!', $op);

Upvotes: 0

pinkal vansia
pinkal vansia

Reputation: 10310

If number of parameters are not too many it is convenient to pass parameters like...

php index.php process process_opk/par1/par2/par3...

and in controller

<?php

class Process extends CI_Controller {

function __construct() {
    parent::__construct();       
}

public function index()
{
    $this->process_opk();
}

public function process_opk($par1 = -1,$par2 = -1,$par3 = -1)
{
  //process
}
?>

Upvotes: 2

Dmitriy Lapin
Dmitriy Lapin

Reputation: 36

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.

Upvotes: 2

stormdrain
stormdrain

Reputation: 7895

I guess CLI changes how variables are passed through CLI, am I wrong?

No.

https://stackoverflow.com/a/2873015/183254

Your solution seems to be the best route although not sure base64 is necessary (it might be esp if you have wonky characters).

Upvotes: 1

Related Questions