I want to call an external php script with applescript and alfred. Currently I open the Safari with the url to the php script. This is very annoying. Is there a way to call the php script without open the Safari?
Best regards,
emha
PHP scripts / commands can be ran from a standard shell prompt:
do shell script "php -q " & quoted form of posix path of phpScriptPath
Source: here
EDIT:
You can use cURL from the shell, and dump the output. This is kind of like just pinging your script, which I'm guessing is what you want to do. Just replace http://www.google.com with the path to your script. And if you omit >/dev/null 2>&1, you can get cURL's output. This is nice because you can add flags to curl to show headers, do post / get, etc.
do shell script "curl http://www.google.com >/dev/null 2>&1"
Related
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));
?>
I will launch a bash scrip for php.
the echo of my bash displays the correct code syntax but does not launch ...
#!bin/bash
var1=$1
var2=$2
config=$("xfreerdp /v:server.domain.tld /u:$1 /p:$2 /load-balance-info:\"tsv://ms terminal services plugin.1.programmes_remot\")
eval "$config"
I turn your senses after my php code
system("/var/www/test/./script.sh $var1 $var2");
I just tested the method 2 no error but its not launch xfreerdp this is what I put
$cmd='xfreerdp /v:server.domain.tld /u:'.$var1.' /p:'.$var2.' /f /cert-igore -menu-anims /network:lan load-balance-info:"tsv://MS Terminal Services Plugin1.Programmes_Remot"';
exec('export display=guilinuxbox:0.0 $cmd');
but this does not launch
A slash is missing in the she-bang: #!/bin/bash
Anyway, your script seems rather complicated. You could just write:
#!/bin/bash
xfreerdp /v:server.domain.tld /u:$1 /p:$2 /load-balance-info:\"tsv://ms terminal services plugin.1.programmes_remot\"
or call xfreedb directly in your PHP script:
$var1 = escapeshellarg($var1);
$var2 = escapeshellarg($var2);
system("xfreerdp /v:server.domain.tld /u:$var1 /p:$var2 /load-balance-info:\"tsv://ms terminal services plugin.1.programmes_remot\"");
Note, that you should always escape the variables that you put in a shell command for security reasons.
Edit:
Ah okay, it’s a GUI/X11 application. Are you running the PHP script on a Web server? Add 2>&1 to the command line string like this system("... 2>&1") to see any error messages in the HTML output.
I guess, you also need to explicitly grant access to the screen.
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've created a script that uses psexec to call another script which calls psexec to run a command line program of mine.
The reason for so many calls to psexec and other scripts is solely so that my PHP script doesn't have to wait for the process to finish before finishing it's output to the browser.
Is there a way I can do this without needing to use psexec? I'm having issues with psexec so I'd like to just completely remove it from my program.
I'm running Windows 2008
EDIT: I changed the title, I guess this would be a more accurate title. I found out the If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends. on php.net's page on exec(), but wasn't sure how to do that.
Include a redirection in the command line:
exec('program.exe > NUL')
or you could modify your program to explicitly close standard output, in C this would be
CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE));
It is possible (the documentation doesn't say) that you might need to redirect/close both the standard output and standard error:
exec('program.exe > NUL 2> NUL')
or
CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE));
CloseHandle(GetStdHandle(STD_ERROR_HANDLE));
$command = 'start /B program.exe > NUL';
pclose( popen( $command, 'r' ) );
For more info:
http://humblecontributions.blogspot.com/2012/12/how-to-run-php-process-in-background.html
Try the Windows 'start' command: http://technet.microsoft.com/en-us/library/cc770297%28WS.10%29.aspx
Using start /B has certain limitations when calling scripts that are spawning sub-processes.
The following method spawns a sub-process using PowerShell to handle such cases:
function execInBackgroundWindows($filePath, $workingDirectory, $arguments)
{
$cmd = "powershell.exe Start-Process -FilePath $filePath -WorkingDirectory $workingDirectory -ArgumentList '$arguments'";
shell_exec($cmd);
}
execInBackgroundWindows('curl.exe','c:\temp','-X POST -H "Content-Type: application/json" -d #test.json http://127.0.0.1:4000/myapp');
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!!