Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exec with In Yii2

I'm working on a project using PHP and Yii2 framework. It has a task that requires making many requests to get data through API and it takes like one minute to finish. To avoid keeping the user waiting, I tried to use exec() function and redirect the output to other file. To make better UX

I tried this code in separated PHP files (outside Yii) and it work perfectly.

exec("php process.php > output.php 2>&1 & echo $!", $output);

once I tried to execute it in Yii using the following, it doesn't work.

public function actionIndex() {
    $url = Url::to(['user/dofile'], TRUE);
    exec("php $url > testoutput.php 2>&1 & echo $!", $output);
    return $this->render('index');
}

The error appears

Could not open input file: http://localhost/weez/frontend/web/index.php?r=user%2Fdofile

Is there a way to make it work in Yii actions?

Is there alternative ways to accomplish that

Thank you

like image 829
Nabeel Avatar asked Apr 06 '26 13:04

Nabeel


1 Answers

You can't execute like php $url and expect php to parse it. try curl -O $url.

But a better option to supplement the original issue ("avoid keeping the user waiting"), try implement "queuing".

like image 175
jovani Avatar answered Apr 09 '26 01:04

jovani