3

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.

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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
}
?>

Comments

1

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).

1 Comment

Well, CI doesn't allow { } = by default and probably more too, so not worth the risk. This is working for what I need atm.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.