天天看點

PHP Console工具使用分享

PHP Console工具使用分享

您的評價: 不錯  收藏該經驗    

功能介紹

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

​<?php​

​require_once​

​​

​(__DIR__ . ​

​'/../src/PhpConsole/__autoload.php'​

​);​

​// Call debug from PhpConsole\Handler​

​$handler​

​ ​

​= PhpConsole\Handler::getInstance();​

​$handler​

​->start();​

​$handler​

​->debug(​

​'called from handler debug'​

​, ​

​'some.three.tags'​

​);​

​$array​

​= ​

​array​

​(​

​'test'​

​=> 1,​

​'test2'​

​=> 1,​

​'key'​

​=> ​

​array​

​(​

​1, 2, 3, 4,​

​), ​

​);​

​$handler​

​->debug(​

​$array​

​, ​

​'test.wiki.wade.zhan'​

​);​

輸出調試資訊到控制台,如下圖:

PHP Console工具使用分享

原理介紹

PHP Console工具通過将調試資訊輸出到http響應頭PHP-Console,然後PHP Console插件分析響應頭PHP-Console字元串輸出相應調試資訊。

PHP Console工具使用分享

附錄

PHP Console提供了通過密碼來保護調試資訊的功能,如下例,在伺服器端設定密碼:

18

19

20

21

​<?php​

​require_once​

​(__DIR__ . ​

​'/../src/PhpConsole/__autoload.php'​

​);​

​$password​

​= ​

​'test'​

​;​

​$connector​

​= PhpConsole\Connector::getInstance();​

​$connector​

​->setPassword(​

​$password​

​);​

​// Call debug from PhpConsole\Handler​

​$handler​

​= PhpConsole\Handler::getInstance();​

​$handler​

​->start();​

​$handler​

​->debug(​

​'called from handler debug'​

​, ​

​'some.three.tags'​

​);​

​$array​

​= ​

​array​

​(​

​'test'​

​=> 1,​

​'test2'​

​=> 1,​

​'key'​

​=> ​

​array​

​(​

​1, 2, 3, 4,​

​), ​

​);​

​$handler​

​->debug(​

​$array​

​, ​

​'test.wiki.wade.zhan'​

​);​

此時可以看到隻有當用戶端輸入正确的密碼:

PHP Console工具使用分享

此時響應頭部才會輸出相應的調試資訊:

​HTTP​

​/1​

​.1 200 OK​

​Server: Tengine​

​/2​

​.0.3​

​Date: Tue, 28 Oct 2014 12:36:04 GMT​

​Content-Type: text​

​/html​

​Connection: keep-alive​

​X-Powered-By: PHP​

​/5​

​.3.29​

​PHP-Console-Postpone: {​

​"protocol"​

​:5,​

​"isPostponed"​

​:​

​true​

​,​

​"id"​

​:​

​"6957661441226218549514727634"​

​}​

​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}]}​

​Content-Length: 0​