Exploring PHP's built-in Web Server (2) Customizing the PHP console output console function, webconsole_PHP tutorial

WBOY
Release: 2016-07-12 08:58:38
Original
1267 people have browsed it

Exploration of PHP’s built-in Web Server (2) Customizing the PHP console output console function, webconsole

We are developing the server side of the APP. When jointly debugging with the APP, we usually need Track URL requests and parameter reception in real time.

But PHP does not have a proprietary console output function like Python or Java. Python’s print() and Java’s System.out.println(), PHP’s echo, print and other functions can be used directly using php -f index. When using php, it can be printed on the shell screen, but when cli_server is running, it is not enough to print the debugging information only in the shell and not display it in the browser.

After searching with Du Niang for a long time, there is very little information on the PHP built-in server cli mode on the Internet, and I can't find any useful information at all.

After consulting the official PHP documentation, I finally thought of PHP’s Standard Output StreamSTDOUT

php://output is a write-only data stream that allows you to write to the output buffer in the same way as print and echo

When running in cli mode, as long as data is written to STDOUT, echo or print will be immediately printed to the SHELL client:

So, we can write a custom function to encapsulate the data input of the sdtout standard output stream, which is equivalent to printing the scheduling information we want to display to the shell at any time:

<span><span>1</span> <span>/*</span>
<span>2</span> <span>* cli模式或者内置server打印调试信息,而不在浏览器输出
</span><span>3</span> <span>* param fixed $data    参数可以是除了对象以外的所有数据类型,比如:字符串,数组,jason等
</span><span>4</span> <span>*/</span>
<span>5</span> <span>function</span> console(<span>$data</span><span>){
</span><span>6</span>      <span>$stdout</span> = <span>fopen</span>('php://stdout', 'w'<span>);
</span><span>7</span>      <span>fwrite</span>(<span>$stdout</span>,json_encode(<span>$data</span>)."\n");   <span>//</span><span>为了打印出来的格式更加清晰,把所有数据都格式化成Json字符串</span>
<span>8</span>      <span>fclose</span>(<span>$stdout</span><span>);
</span><span>9</span> }</span>
Copy after login

When you need to print the current debugging information after each request of the built-in Server: just call console() directly

For example:

Print all received GET or POST parameters:

console($_GET);

console($_POST);

 

Print string:

console("hello php console debug");

 

You no longer have to envy the console output of Python and Java, PHP now also has a console output function

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1102848.htmlTechArticleExploration of PHP built-in Web Server (2) Customizing the PHP console output console function, webconsole We are developing the APP server When jointly debugging with APP, it is usually necessary to track URL requests and...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!