I am wondering if a PHP script can be executed from a shell command line.
Does a script executed from shell have the exact functionality if executed from the browser? or is there a difference in coding.
Is it better to run a script from shell for performance and also is it better to run it from windows or unix/linux
I am asking all these questions because, I am suppose to develop a PHP script that can fetch some data from http headers of some urls listed in a MySQL db and then store the data in the database.
Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows? all I have at the moment installed is WAMP, which has mysql, php and apache server.
I am sorry for being a novice.
thanks for your kind help
I am wondering if a PHP script can be executed from a shell command line.
Yes
Does a script executed from shell have the exact functionality if executed from the browser? or is there a difference in coding.
It won't have $_REQUEST and friends populated, and $_SERVER won't have server information in it.
Is it better to run a script from shell for performance
Maybe. It avoids the overhead of runnning a webserver. It stops you having cached versions in memory for faster re-execution.
and also is it better to run it from windows or unix/linux
Benchmark it.
I am asking all these questions because, I am suppose to develop a PHP script that can fetch some data from http headers of some urls listed in a MySQL db and then store the data in the database.
There doesn't seem to be any need to involve a web server for that.
Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows?
The standard Windows shell can.
all I have at the moment installed is WAMP, which has mysql, php and apache server.
You'll need the command line version of PHP. I've no idea if WAMP includes it or not.
I am wondering if a PHP script can be executed from a shell command line.
It's possible either by executing:
$ php -f your_script.php
Or by inserting #/usr/bin/env php into the first line of the script and by making it executable.
$ head -n 1 your_sript.php
#/usr/bin/env php
$ chmod +x your_script.php
$ ./your_script.php
Note: this example only works on UNIX systems.
Does a script executed from shell have the exact functionality if executed from the browser? or is there a difference in coding.
You can use the same Syntax/Functions etc. The only difference is that there are command line arguments in $argv and some other values in the $_SERVER variable.
Is it better to run a script from shell for performance and also is it better to run it from windows or unix/linux.
That doesn't really matter. For your usecase you don't really need a webserver, and a full featured GUI. The advantage of having a command line tool is, you can combine your program with other program available like grep etc.
Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows?
You don't need ubuntu, you can also execute a shell script from windows. The PHP executable is located in the %PATH%. This question will help you in order to do this: https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them
Then simply open cmd.exe and execute a script using php -f your_script.php
Yes, PHP can be run from command line.
No, there aren't any differences in coding.
The only difference is that it's not Apache running the script, but the user you are currently logged in as. That could mean different privileges on certain maps and folders.
Yes you can execute PHP from the command line using:
/path/to/php.exe /path/to/script.php
The main difference is that it doesn't run through Apache, so you won't have things that rely on it (like some $_SERVER data).
Also it won't be subject to timeouts on the command line, unless you have a PHP limit set.
Take a look at http://php.net/manual/en/features.commandline.php for more info.
Related
If I have a .php script, I can run it from the terminal using
php myscript.php
In my case I'm mostly working on macOS and Linux (Ubuntu 18.04) but generally this works everywhere where PHP is installed.
However, I would prefer to make my .php script executable (i.e. chmod u+x myscript.php) so I can run it directly.
In order for this to work in any environment, I have to put a PHP shebang at the top of my script. Problem: the php binary has different locations on different platforms.
After searching around through similar stackexchange topics, the closest, most 'universal' way of doing this seems to be:
#!/usr/bin/env php
Is this correct, or what's the idea behind this? In what situations can this fail, and are there any fallbacks?
Trying to mess around with PHP, but I don't want to install IIS or Apache and was hoping for a small interpreter that I can pass the scripts to and have them run in like a console or something. Much like Lua does. Does this exist? When I go to download PHP it seems to only talk about running it on IIS or Apache.
PHP can be used on the command line. Just download and extract the executable.
Running can be done 3 ways: a file, supplied code or in an interactive shell
php file.php
php -r "echo 'hello';"
php -a
You can also install a pre-packaged server (e.g. XAMPP) or run your code online on various places (e.g. phpfiddle.org)
With PHP 5.1+ you have an interactive shell too:
launch php with -a parameter
php.exe -a
http://php.net/manual/en/features.commandline.interactive.php
I believe that PHP v5.4 comes with its own webserver built-in. Install that version and you should be fine. Though I really would like to know why you have problem with installing Apache (IIS sucks though).
EDIT:
As a few others have said, you can run PHP from the command line.
check out easyphp, a server for php development
http://php.net/cli
Alternatively, you can write your PHP scripts as you would normally (with some limitations),
And use the following command line:
C:\php.exe -f "D:\phpFile.php"
dtech's answer seems most fitting for the simple script-running things you're looking for. If you decide you want an actual web-server, with as little setup as possible, look at "XAMPP".
Say I want to execute an ant command on a linux server using php on a website...
Can I do say, /home/user/apache-ant-1.8.2/bin/ant compile - where compile is the command?
Basically I need to start up a few things with ANT in a few different directories. I would like to do this from a php webpage, can shell_exec perform this?
Yes, shell_exec can run any command (with appropriate permissions).
I have 55 php files that I would like to run simultaneously from the command line. Right now, I am running them in multiple CLI windows using the code:
php Script1.php
I would like to be able to call one single php file that would execute all 55 php files simultaneously. I have been reading about how to make the command line not wait for the output, but I can't seem to make it work.
This thread:
How to run multiple PHP scripts from CLI
suggests putting an & at the end of the command to run the command in the background, but using the xampp CLI this doesn't seem to do anything.
Any ideas greatly appreciated.
Brian
By mentioning XAMPP, I assume you are on windows. I think what you need is the start command. You probably need start php Script1.php. For more info, do a
start /?|more
Linux
Apart from adding a &, you also need to redirect output to somewhere - otherwise your php process waits until the other process finished, because there could be more output:
exec('/path/to/program & > /dev/null 2>&1')
You could use the php forking mechanism. Read about it here: http://php.net/manual/en/function.pcntl-fork.php
I have a (possibly dumb) question.
I have a script made in php, constructed for cli usage. Works fine when I run it from the command line, no problem there. The problem is that the site I'm working on has ssh restrictions on the hosting server and I cannot ssh there to run it. Hence my question: how can I run the script from another php that is web-accessible? Already tried with exec(), system(), etc.
The main problem is that I need he $_SERVER['SHELL'] variable set, and when the call is comming from a web browser of course php doesn't set it.
Any ideeas will be greatly apreciated, thanx.
There are many possibilities why exec() and related function calls are not working for you.
Your webhost does not have PHP-CLI installed. Just a webserver module
You need to use the full path to the php binary for lack of a decent shell environment. E.g. /usr/bin/php <script> instead of php <script>.
Your webhost has installed PHP-CLI on a non-standard path (e.g. /usr/local/bin/php, or /opt/php5/php)
The webserver user does not have rights to access the php binary
Et cetera..
maybe update the php script to be both an include and a cli script.
use
__FILE__
to check if it's a file, then read the params. otherwise do nothing.
and as an include just call the function you want directly.