Laravel Queue download file using CURL - php

I'm trying to generate the file using curl. When I try executing the command via terminal it works fine.
But when I try to implement it on laravel using a queue, the script runs but the downloaded file is corrupt.
As you can see from the "Time Spent" it doesn't even have value compared to the one executed from terminal. And the file is there but obviously corrupt since laravel already finished the command even before it started.
Here's my code on the handle() function of GeneratBukuVot Job:
$path = Storage::path('public/test.pdf');
$pentaho = 'http://192.168.0.17:8080/pentaho/api/repos/:home:admin:anggaran_hasil_and_belanja_param.prpt/generatedContent?output-target=pageable/pdf&search_year=2019';
$cmd = 'curl -u"admin:password" "'.$pentaho.'" -o "'.$path.'"';
$output = shell_exec($cmd);
I'm using Laravel version 5.8. Please, help.

Related

Run .sh file using exec Laravel PHP

I am trying to run a .sh file that will import a excel file to my database. Both files are in same directory inside the public folder. For some reason the exec command isn't being executed or neither any error occurs.
.sh file colde:
IFS=,
while read column1
do
echo "SQL COMMAND GOES HERE"
done < file.csv | mysql --user='myusername' --password='mypassword' -D databasename;
echo "finish"
In my php file i have tried following:
$content = file_get_contents('folder_name/file_name.sh');
echo exec($content);
And:
shell_exec('sh /folder_name/file_name.sh');
Note: I can directly execute the sh file from gitbash however I want it to be done using function in Laravel controller. I'm using windows OS.
you can use Process Component of Symfony that is already in Laravel http://symfony.com/doc/current/components/process.html
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
$process = new Process('sh /folder_name/file_name.sh');
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
All of these answers are outdated now, instead use (Symfony 4.2 or higher):
$process = Process::fromShellCommandline('/deploy.sh');
Or
$process = new Process(['/deploy.sh']);
https://symfony.com/doc/current/components/process.html
I know this is a little late but I can't add a comment (due to being a new member) but to fix the issue in Windows " 'sh' is not recognized as an internal or external command, operable program or batch file." I had to change the new process from:
$process = new Process('sh /folder_name/file_name.sh');
to use the following syntax:
$process = new Process('/folder_name/file_name.sh');
The only problem with is that when uploading to a Linux server it will need to be changed to call sh.
Hope this helps anyone who hit this issue when following the accepted answer in Windows.
In Symfony 5.2.0 that used by Laravel 8.x (same as current Symfony Version 6.0 used by Laravel 9.x), you need to specify the sh command and your argument, in your case:
use Symfony\Component\Process\Process;
$process = new Process(['/usr/bin/sh', 'folder_name/file_name.sh']);
$process->run();
The system will find folder_name/file_name.sh from your /public folder (if it executed from a url), if you want to use another working directory, specify that in the second Process parameter.
$process = new Process(['/usr/bin/sh', 'folder_name/file_name.sh'], '/var/www/app');
And the /usr/bin/sh sometimes have a different place for each user, but that is a default one. Type whereis sh to find it out.

Unable to execute Python script from PHP shell_exec

I'm trying to execute a python (Python 3) script from the PHP shell_execfunction but it doesn't seem to be working, I'm trying it on a windows system but it will be in CentOS 7 in production.
In the below code snippet nothing happens when the PHP runs. But it I use the $response = shell_exec("php -v"); line it displays the PHP version ok. I've also tried running the python script directly on the command line and it runs fine with no errors.
Code
$command = "\"C:\Programs\Python\Python36-32\python.exe\" \"E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py\" \"https:/www.google.ie\" 1";
$response = shell_exec($command);
//$response = shell_exec("php -v");
echo $response;
Command Line
"C:\Programs\Python\Python36-32\python.exe" "E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py" "https:/www.google.ie" 1
Update #1
I've gotten the python script to run via PHP, but the python script crashes once it hits driver = webdriver.PhantomJS(), the import exists in the python script from selenium import webdriver and all this functionality works fine once called directly via the command line but doesn't seem to work once the python script is called from PHP.
Here's a sample script that prints out test 1 but not test 2 when called from the PHP script.
from selenium import webdriver
print("test 1")
driver = webdriver.PhantomJS()
print("test 2")
driver.quit()
Update #2
Looks like it was an issue with paths, there is a file created in the python script that was using a relative path, once I changed the path to be a full path the script ran ok both in the command line and when being called via the PHP script.
You need to scape the command string.
Try using escapeshellarg function.
$command = escapeshellarg('C:\Programs\Python\Python36-32\python.exe "E:\htdocs\dev\_nodes\jobs/jobCheck.py" "https:/www.google.ie" 1');
$response = shell_exec($command);
//$response = shell_exec("php -v");
echo $response;
http://php.net/manual/pt_BR/function.escapeshellarg.php

Keep bash shell functions between shell_exec() requests

I have a PHP script that is run via CLI. In turn I want that script to call a bash script, but preferably I would like to break up the BASH requests so I can see the action as it is happening.
I can seem to set Environmental variables and they seem to exist between shell_exec() functions. But even when I have a source file like:
source ./bashes/functions.sh
And in the source file I use "export -f function-name" to export the functions in the script before executing the next line, the next line does not see the functions.
$file = file('./bashes/bash_testing.sh',FILE_SKIP_EMPTY_LINES);
//we want to watch this in realtime so write each line seperately to shell.
foreach($file as $command) {
$output->writeln(shell_exec($command));
}
The function $output->writeln is a helper function just to echo the returned result. But basically the error I get is
sh: now: command not found
now is defined as a function in the included bash_testing.sh shell script.
Anyone know how I can resolve this issue?
Here is the source to the ./bashes/functions.sh file:
function now {
date -u +%T
}
export -fx now
There is a way to maintain a single bash shell, execute commands and handle the return. I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS
I would suggest not triggering a bash script but rather trigger the induvidual commands. That way you can handle the return and not have to build exception handling in bash.
After downloading you would simply use the following code:
//get a real bash shell.
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', false);
$return1 = $shell->exeCmd($command1);
//logic to handle the return
$return2 = $shell->exeCmd($command2);
//logic to handle the return
..... etc

PHP Execute bat file on windows server

I have a PHP file that creates a pdf fill form and uses pdftk to merge the data into the template.
I am having trouble getting php to execute the batch file in order to run the program and merge it.
$current = '\\oma-entfs-002\aps\wwwroot\tuition\uploads\';
My PHP code:
$WshShell = new COM("WScript.Shell");
$WshShell->exec($current.'makePDF.bat ' .$fdf_file.' '.$newPDF);
The Batch File:
pushd \\oma-entfs-004\APS\wwwroot\tuition
pdftk uploads/Educational_Assistance_Request_Form_North_America.pdf fill_form uploads/%1 output uploads/%2 need_appearances
popd
Both COM and Exec are enabled on the server as far as I can tell.
When I run the batch file from command line, it works just fine so I think there is something with PHP not running the file correctly.
Any suggestions on the best way to debug this and identify the root cause?

Running oowriter headless command using php exec() not executing

I'm having issues using oowriter to convert a doc to a pdf using php exec(). The line i'm running below works fine in CLI, just not in php using exec().
$stdin = "/usr/bin/oowriter --headless --convert-to pdf -outdir /var/data/uploads/ /var/data/uploads/lorem.docx";
exec($stdin,$stdout,$return_int);
$stdout returns an empty Array and $return_int returns 0.
I've searched around and the only possible solution I could find was adding the path to oowriter (which you can see i have done). Is there a permissions issue running this command as the apache user? There are a few posts on exec() not working in general, but I can successfully run commands for imagemagick using this method. Therefore it seems to me that this an application specific configuration issue with openoffice.
Appears to be a duplicate of the following, but no answers have been posted:
unable to run oowriter as web user
FYI I'm using CentOS V6.1 and PHP v5.3.3. Thanks.

Categories