查看: 738|回复: 0

[PHP学习] PHP Console工具使用分享

发表于 2016-12-14 08:00:12

PHP Console:https://github.com/barbushin/php-console#php-console-server-library

功能介绍

PHP Console工具和FirePHP功能相似,提供以下功能:

Handle PHP errors, dump variables, execute PHP code remotely in Google Chrome
Demo

首先在Chrome中安装PHP Console插件:

https://chrome.google.com/webstore/detail/php-console/nfhmhhlpfleoednkpnnnkolmclajemef

其次在php代码中引入PHP Console库,然后调用输出相应调试信息:

如下例index2.php:

  1. <?php
  2. require_once(__DIR__ . '/../src/PhpConsole/__autoload.php');
  3. // Call debug from PhpConsole\Handler
  4. $handler = PhpConsole\Handler::getInstance();
  5. $handler->start();
  6. $handler->debug('called from handler debug', 'some.three.tags');
  7. $array = array(
  8. 'test' => 1,
  9. 'test2' => 1,
  10. 'key' => array(
  11. 1, 2, 3, 4,
  12. ),
  13. );
  14. $handler->debug($array, 'test.wiki.wade.zhan');
复制代码

输出调试信息到控制台,如下图:

20141028202013805.png

原理介绍

PHP Console工具通过将调试信息输出到http响应头PHP-Console,然后PHP Console插件分析响应头PHP-Console字符串输出相应调试信息。

20141028202101906.png

附录

PHP Console提供了通过密码来保护调试信息的功能,如下例,在服务器端设置密码:

  1. <?php
  2. require_once(__DIR__ . '/../src/PhpConsole/__autoload.php');
  3. $password = 'test';
  4. $connector = PhpConsole\Connector::getInstance();
  5. $connector->setPassword($password);
  6. // Call debug from PhpConsole\Handler
  7. $handler = PhpConsole\Handler::getInstance();
  8. $handler->start();
  9. $handler->debug('called from handler debug', 'some.three.tags');
  10. $array = array(
  11. 'test' => 1,
  12. 'test2' => 1,
  13. 'key' => array(
  14. 1, 2, 3, 4,
  15. ),
  16. );
  17. $handler->debug($array, 'test.wiki.wade.zhan');
复制代码

此时可以看到只有当客户端输入正确的密码:

20141028204037953.png

此时响应头部才会输出相应的调试信息:

  1. HTTP/1.1 200 OK
  2. Server: Tengine/2.0.3
  3. Date: Tue, 28 Oct 2014 12:36:04 GMT
  4. Content-Type: text/html
  5. Connection: keep-alive
  6. X-Powered-By: PHP/5.3.29
  7. PHP-Console-Postpone: {"protocol":5,"isPostponed":true,"id":"6957661441226218549514727634"}
  8. PHP-Console: {"protocol":5,"auth":{"publicKey":"bf802ef9f6d61a5d4a720892a79bf8285d92c31c2a99be2931b504dc54eeb209","isSuccess":true},"docRoot":"\/usr\/local\/wwwroot\/dokuwiki","sourcesBasePath":null,"getBackData":null,"isLocal":false,"isSslOnlyMode":false,"isEvalEnabled":false,"messages":[{"type":"debug","tags":["some","three","tags"],"data":"called from handler debug","file":null,"line":null,"trace":null},{"type":"debug","tags":["test","wiki","wade","zhan"],"data":{"test":1,"test2":1,"key":[1,2,3,4]},"file":null,"line":null,"trace":null}]}
  9. Content-Length: 0
复制代码
来自:http://blog.csdn.net/billfeller/article/details/40554625


回复

使用道具 举报