cud_programmer
cud_programmer

Reputation: 1264

Output HTML from PHP CLI execute

I hope this question makes sense - it might be that I don't fully understand what is going on.. but after a considerable amount of googling to no avail here goes:

What I'm trying to do is execute a PHP file on the command line using php filename.php and get the output as HTML - so it could be displayed as a webpage. Currently the command line is just returning raw text output with no HTML tags. Currently the only line of code in PHP file is: <?php phpinfo() ?>

Is this possible? If not, how does MAMP etc execute that file to produce a HTML output?

Thanks!

Upvotes: 4

Views: 5757

Answers (7)

richhallstoke
richhallstoke

Reputation: 1601

Where phpinfo.php contains:

<?php phpinfo();

From the console, to generate an HTML file of the phpinfo() output:

php-cgi phpinfo.php > phpinfo.html

The important thing is to use php-cgi instead of php if you want the output to contain HTML when executed from the console.

Upvotes: 1

muglio
muglio

Reputation: 2057

If you use php-cgi instead of php from the CLI your program will run as if it were running on a webserver. It won't have access to anything in $_SERVER, $_POST, $_GET etc but it will cause phpinfo() to produce HTML instead of plain text.

Upvotes: 7

Pankaj Khairnar
Pankaj Khairnar

Reputation: 3108

phpinfo() function is smart enough to identify where request is coming from if it is coming from CLI then it doesn't return the html but when request is coming from apache it merge it with html and sends the output.

there is a example listed here for converting cli raw output of phpinfo() to html but I have not tried it.

Upvotes: 2

Ed Heal
Ed Heal

Reputation: 59987

HTML is text (ASCII for example) output that contains sequence of characters that have special meaning.

Do something like this

php filename.php > webpage.html

Then view webpage.html with your browser.

As to the function phpinfo() I am not quite sure what environment/global variables are required to produce output.

Upvotes: 3

Alister Bulman
Alister Bulman

Reputation: 35139

HTML is just text. There's nothing special about it.

<?php
  echo '<p>this is html, you'll want the header, body tags, and content.';
?>

Upvotes: 1

Karl Laurentius Roos
Karl Laurentius Roos

Reputation: 4399

phpinfo() does not output HTML if it's executed from the command line. HTML will however be returned as text when executed in the command line.

Upvotes: 1

Marcin Orlowski
Marcin Orlowski

Reputation: 75619

You have to produce HTML, not MAMP, LAMP or anyone else. You run the program and the program produces its output. So if you want to see HTML, just spit out HTML.

Upvotes: 0

Related Questions