TechTalkz.com Logo

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > PHP

Notices

Running PHP via CLI

PHP


Reply
 
Thread Tools Display Modes
Old 07-12-2007, 04:58 PM   #1
Kim
Guest
 
Posts: n/a
Running PHP via CLI

I made a php script with a HTML form (POST) which takes many options
and based on the options, it connects to a database file and create
some files from the results. This works fine when using a browser.

Now, I am being asked if it can run via CLI. My first reaction was
"no", but that was before I even had looked into the matter.
Running via CLI is preferred instead of having to install a webserver
solution like WAMP. Also, the target machine which should run the PHP
script is a standalone (no network of any kind).

After reading some about PHP in CLI mode, and experiments have I come
to this conclusion:
* php.exe does not support GET, POST or HTML codes (it will displayed
as raw text)
* php-cgi.exe does support GET but still not POST or HTML codes.
This means you can parse parameters to your php script, like: <php-cgi
path> -n -f <php script> para1=value1 para2=value2 etc. Full access to
$_GET.

I still have not found a way to run this script via CLI.

Do I need to re-write the whole thing specially for CLI mode ?
Is there another way to parse parameters via CLI mode ?
  Reply With Quote
Old 07-12-2007, 04:59 PM   #2
Csaba Gabor
Guest
 
Posts: n/a
Re: Running PHP via CLI

Kim wrote:
> I made a php script with a HTML form (POST) which takes many options
> and based on the options, it connects to a database file and create
> some files from the results. This works fine when using a browser.
>
> Now, I am being asked if it can run via CLI. My first reaction was


I assume you mean you are being asked to run it from the command line

> "no", but that was before I even had looked into the matter.
> Running via CLI is preferred instead of having to install a webserver
> solution like WAMP. Also, the target machine which should run the PHP
> script is a standalone (no network of any kind).


as opposed to having a nice web interface for it on a
standalone machine. As you probably know, you can
configure your browsers so that putting in word like
phpadmin into the address bar will redirect it to a
local file (instead of the web at large).

> After reading some about PHP in CLI mode, and experiments have I come
> to this conclusion:
> * php.exe does not support GET, POST or HTML codes (it will displayed
> as raw text)


Unclear what you mean by this. PHP will print out whatever you tell
it to. But if it's CLI (command line interface), then that output
goes to the command window, where of course it is not interpreted
as HTML. If you are writing script to output to two different types
of media, you need to accommodate both types. Adding <pre>...</pre>
tags is often an easy quick fix during testing when varying output
between the command window and the browser.

> * php-cgi.exe does support GET but still not POST or HTML codes.
> This means you can parse parameters to your php script, like: <php-cgi
> path> -n -f <php script> para1=value1 para2=value2 etc. Full access to
> $_GET.


Are you looking for something like this?

if (!$_SERVER['REQUEST_METHOD'])
foreach ($argv as $arg)
if ($pos=strpos($arg, "="))
$_POST[strtolower(substr($arg,0,$pos))] =
substr($arg,$pos+1);

> I still have not found a way to run this script via CLI.
>
> Do I need to re-write the whole thing specially for CLI mode ?
> Is there another way to parse parameters via CLI mode ?


Just make the $_POST et al. variable yourself.
It's useful to always include operating system configuration,
especially when asking about CLI.
Above code tested on Win XP Pro / PHP 5.2.4

Csaba Gabor from Vienna
  Reply With Quote
Old 07-12-2007, 04:59 PM   #3
Norman Peelman
Guest
 
Posts: n/a
Re: Running PHP via CLI

Kim wrote:
> I made a php script with a HTML form (POST) which takes many options
> and based on the options, it connects to a database file and create
> some files from the results. This works fine when using a browser.
>
> Now, I am being asked if it can run via CLI. My first reaction was
> "no", but that was before I even had looked into the matter.
> Running via CLI is preferred instead of having to install a webserver
> solution like WAMP. Also, the target machine which should run the PHP
> script is a standalone (no network of any kind).
>
> After reading some about PHP in CLI mode, and experiments have I come
> to this conclusion:
> * php.exe does not support GET, POST or HTML codes (it will displayed
> as raw text)
> * php-cgi.exe does support GET but still not POST or HTML codes.
> This means you can parse parameters to your php script, like: <php-cgi
> path> -n -f <php script> para1=value1 para2=value2 etc. Full access to
> $_GET.
>
> I still have not found a way to run this script via CLI.
>
> Do I need to re-write the whole thing specially for CLI mode ?
> Is there another way to parse parameters via CLI mode ?



Running PHP from the command line (terminal window) requires that you
use php.cli (not php.cgi or php.exe). Well, it doesn't require it but
the point is that you cannot use the standard HTML FORMS for your input
and output. Please follow this link and read the whole page...




---
Norm
  Reply With Quote
Old 07-12-2007, 04:59 PM   #4
Norman Peelman
Guest
 
Posts: n/a
Re: Running PHP via CLI

Kim wrote:
> I made a php script with a HTML form (POST) which takes many options
> and based on the options, it connects to a database file and create
> some files from the results. This works fine when using a browser.
>
> Now, I am being asked if it can run via CLI. My first reaction was
> "no", but that was before I even had looked into the matter.
> Running via CLI is preferred instead of having to install a webserver
> solution like WAMP. Also, the target machine which should run the PHP
> script is a standalone (no network of any kind).
>
> After reading some about PHP in CLI mode, and experiments have I come
> to this conclusion:
> * php.exe does not support GET, POST or HTML codes (it will displayed
> as raw text)
> * php-cgi.exe does support GET but still not POST or HTML codes.
> This means you can parse parameters to your php script, like: <php-cgi
> path> -n -f <php script> para1=value1 para2=value2 etc. Full access to
> $_GET.
>
> I still have not found a way to run this script via CLI.
>
> Do I need to re-write the whole thing specially for CLI mode ?
> Is there another way to parse parameters via CLI mode ?



....and as a follow-up, you want want to look at ncurses for display and
user input purposes. It'll give you the DOS like application view. I've
not used it but I believe if you area on Windows it can a bit of a bear
(or may not work at all) but on Linux it seems to work just fine (using
ubuntu and installing php5 and ncurses via the package manager it works
out of the box.

Norm
  Reply With Quote
Reply

Thread Tools
Display Modes



< Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 03:35 AM.


vBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2009, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional