Call to wget from PHP's shell_exec not working - php

I'm trying to run a PHP script locally that scrapes Google with wget and dumps the HTML into temp.html.
Running this command from the terminal works fine:
wget -O temp.html http://www.google.ca
Running this command from PHP also works fine (so it's not a permissions issue):
shell_exec('touch temp.html');
But running this from PHP does not work (does not create temp.html):
shell_exec('wget -O temp.html http://www.google.ca');
Any suggestions? Wrapping that last command in a var_dump() outputs null.
Thanks!

According to man wget, using wget -O temp.html http://google.com takes all documents, concatenates them and prints everything in temp.html, without producing any stdout so PHP's shell_exec doesn't return anything (null).
The content of the scraped webpage should be present in temp.html, but shell_exec("wget ...") does not return anything, as not output is produced.
As you mentioned the webpage you are trying to scrape does not work, maybe they implemented some sort of bot-protection preventing exactly what you are trying.
Edit: You may use - to print everything to stdout instead. So try using shell_exec("wget -O - https://google.com"); should return the content of the requested page to your PHP script.

The simplest solution is to provide full path to the wget binary as it seems the user that runs your script does ot have the same $PATH as you.

How about using file_put_contents & file_get_contents instead? This should work without having to worry about wget.
<?php
$filename = 'temp.html';
$address = 'http://www.google.ca';
file_put_contents($filename,file_get_contents($address));
?>

Related

PHP exec and shell_exec: no output

Yesterday I asked this question
Building g++ via PHP: no build output
That part is solved, but now I now have a similar/related problem.
When I run the compiled executable (see other question) via my browser address bar, the output is printed to the browser window as expected.
However ,I want to use it within PHP and get the output via the return of the exec or shell_exec functions. I have tried various things, including the following:
$output = "test.cgi";
echo shell_exec($output);
echo shell_exec("$output 2>&1");
echo system($output);
Nothing works..
Internally, the executable is c code, compiled by g++, and is using print_f. That should output to STDOUT by default accoding to the specs, so I do not understand why this isn't working.
Can anyone help?
For anyone with the same problem, I found the solution:
exec needed an absolute path to the file

PHP - shell_exec output in browser is empty

I'm running a simple wget command in shell_exec()
wget_file.php
<?php
$command = "wget http://mydomain.co/media/bigbigbig.wav";
$output = shell_exec($command);
echo $output;
?>
According to http://www.php.net/shell_exec I can possibly expect an output:
"The output from the executed command or NULL if an error occurred or the command produces no output."
If I run wget_file.php from the command line, I'll see a display of the wget results.
However, if I run it from a browser, no result is given. (but the file does indeed download successfully)
I plan to execute the wget_file.php by calling via cUrl, while passing the url + path.
But would be nice to get a response from the shell_exec(), after execution is completed.
Does anyone know if I'm just missing something to get an output (running in the browser)?
If I run wget_file.php from the command line, I'll see a display of the wget results
wget doesn't output the file contents to console by default, so presumably you're talking about the status information.
I'd imagine this is output to STDERR rather than STDOUT, and it's STDOUT that shell_exec obtains for you in your PHP script:
when you run your script from the command line, you're still seeing the output because both streams are shared by the single terminal window; it's just that you're seeing it directly from wget and not from your PHP script;
in the case of passing it through Apache and to a browser to satisfy a web request, this terminal context is disconnected from the result the user sees.
In your shell command you can redirect the former to the latter:
$command = "wget http://mydomain.co/media/bigbigbig.wav 2>&1";
The comments on the documentation for shell_exec touch on this, particularly the — er — very first one!

I need to run a python script from php

I have to design a interface using PHP for a software written in python. Currently this software is used from command line by passing input, mostly the input is a text file. There are series of steps and for every step a python script is called. Every step takes a text file as input and an generates an output text file in the folder decided by the user. I am using system() of php but I can't see the output but when I use the same command from command line it generates the output. Example of command :
python /software/qiime-1.4.0-release/bin/check_id_map.py -m /home/qiime/sample/Fasting_Map.txt -o /home/qiime/sample/mapping_output -v
try this
$script = 'software/qiime-1.4.0-release/bin/check_id_map.py -m /home/qiime/sample/Fasting_Map.txt -o /home/qiime/sample/mapping_output -v';
$a = exec($script);
If you are not on windows, have you tried adding 2>&1 (redirect stderr to stdout) to the end of the command?
$output = system("python /software/qiime-1.4.0-release/bin/check_id_map.py -m /home/qiime/sample/Fasting_Map.txt -o /home/qiime/sample/mapping_output -v 2>&1", $exitcode);
Found from http://www.php.net/manual/en/function.system.php#108713
Also the doc says that it
Returns the last line of the command output on success, and FALSE on
failure.
So if you are trying to get multiple lines, you may need to redirect it to a file and read that in.
instead of system() try surrounding the code in `ticks`...
It has a similar functionality but behaves a little differently in the way it returns the output..

Why isn't this shell command giving the desired result?

I am using the wkhtmtoimage library on my server, and I have managed to get the output for the following command when I am running in PuTTY:
wkhtmltoimage www.google.com test.jpg
But, when I use the following shell command I don't get the output, and I don't know why.
$filnename = "test.jpg";
$url = "http://www.google.com";
shell_exec("wkhtmltoimage $url $filename");
Even I tried with this variation instead, but still without getting the desired result:
shell_exec("/usr/local/bin/wkhtmltoimage $url $filename");
What am I doing wrong?
Edit:
I downloaded the Linux binary and put it into the folder then I ran it.Whether I need to restart the server the changes affected?
shell_exec command is allowed because we used it for ffmpeg(already installed one)
I cannot give an exact solution but if you can try out what I write I might be of help. I will list more solutions if the one I am providing does not work.
Solution proposal 1:
Use quotes around url and filename
shell_exec("wkhtmltoimage \"$url\" \"$filename\"");
Solution proposal 2:
Try redirecting streams to files and type the contents.
shell_exec("wkhtmltoimage \"$url\" \"$filename\" > output.txt 2> error.txt");
May be user under which PHP (or Apache) runs have no permissions to run this tool. Try printing output of this command or just use passthru() to directly print output of command.
Another solution is to pass output to files as #Cem_Kalyoncu wrote.

Can't execute PHP script using PHP exec

I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. After much struggling, I reduced this to the classic hello world example. The calling script looks like:
exec('/usr/bin/php /home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &');
When I run this, the output execoutput.txt contains a copy of the invoking script page, not hello world as I expected.
Why can't I get this PHP script to execute using exec? Note that when I change the command to something like ls -l, the output is a directory listing as expected. btw, in case it matters, I did chmod the called script to 755...
Update - I moved the exec call to the end of the calling script and at least now I don't see the calling script executed in the output. Thx to posters and I will try some of these ideas.
Help!
Thanks
Steve
I had this issue also and it turns out this is a bug in php (#11430). The fix is to use php-cli when calling another php script within a php script. So you can still use exec but rather than use php use php-cli when calling it in the browser:
exec("php-cli somescript.php");
This worked for me.
What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.
if you just want the script's output, try:
exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &')
however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:
exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')
the above should only output the "Hello World" to the execoutput.
Edit:
Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:
alias pexec='php /home/quote2bi/tmp/helloworld.php'
then
exec('pexec > /tmp/execoutput.txt 2>&1 &')
it seems to be a problem with the way exec handles input as opposed to the shell itself.
-John
The problem is with PHP itself, it treats everything as $argv in the script. It doesn´t redirect the output to a file ou to /dev/null.
I faced the same problem some time ago. What I did is to create a runscript.php in /opt/php-bin and then inside this script run what It should be running. Something like this:
$script = $argv[1]
$params = implode(' ', array_slice($argv, 2));
$cmd = "{$script} {$params} > /dev/null &";
$output = array();
$return = 0;
exec("php {$cmd}", $output, $return);
exit((int)$return);
And then you call it using:
exec('/opt/php-bin/runscript.php /path/to/your/script.php arg1 arg2')
It´s the only way I managed to get this working.
To avoid the stated problems of PHP in this area, why not put this in inside a shell script? PHP can then execute the shell script which has all the redirections handled internally.
If you need to dynamically change things, then why not write the shell script and then execute it (and of course, clean up afterwards)?
if you are just simply running a php script one possible way to execute the entire code is to use the include() that will run the php file and output any results. You cannot direct the output to a text file but it should appear in the browser window if you're Hello World php script looks like
<?php echo "Hello World!"; ?>
then it will spit that out in the browser. So your second code would look like
<?php include("helloWorld.php"); echo " PHP ROCKS";?>
resulting in a page that would look like,
Hello world! PHP ROCKS
This runs as if you run the script from browser.
This came across while working on a project on linux platform.
exec('wget http://<url to the php script>)
Hope this helps!!

Categories