4

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!

3
  • 1
    You do realize that in command line interface you don't have any type of HTML rendering engine available? You can only see text, and HTML is text essentially. Commented Nov 23, 2012 at 11:41
  • 1
    ah, no, I didn't realise that - but I guessed something like that was going on. Yes, when I'm talking about HTML I meant I wanted an output just like viewing the source of a webpage that was displaying the output from phpinfo(). Is there any other way of executing PHP with HTML rendering engine without using MAMP etc? Commented Nov 23, 2012 at 11:45
  • No, there isn't. The best what you can achieve is creating a screenshot or PDF using webkit from CLI, which is more of a hassle than installing Apache and using your browser to view the HTML output. Commented Nov 23, 2012 at 11:50

7 Answers 7

7

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 Comments

this won't work as you are just outputting result of php file in to the html file, he needs a html embedded output
@PankajKhairnar - Please read the last statement wrt phpinfo. BTW - see below.
2

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.

Comments

1

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.

3 Comments

I see - is there anyway to get it to output text containing HTML tags, just like if I was to go to view source of a webpage that was displaying phpinfo() (with correct stylesheet, visual formatting etc)?
I'm not sure which method phpinfo() uses internally to determine if it's a CLI request or an HTTP request but it should be possible to spoof. Otherwise you could make an HTTP request from your script to a file which runs phpinfo(), then it would return the HTML.
@cud_programmer - There is nothing special about HTML. Just a text file that adheres to some conventions. As to phpinfo I do not know what goes on under the hood. Perhaps set the global variables as if it was a server running it. Running phpinfo from a server will list them for you. Just set them to the same values at the start of you script to run it from the command line.
1

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.';
?>

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.