20
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

超やっつけでネットワーク経由で var_dump 的なことをする

Posted at

単純に var_dump() では困るとき(Ajax とか)。

ファイルに出力したり、

FirePHPChrome Logger を使ってみたり、

いろいろ方法はあると思いますが、準備とか無しでもっとやっつけでやりたいとき。

次のような関数をどこかで定義します。

<?php
function trace($data)
{
    static $sock;
    if (isset($sock) === false){
        $sock = fsockopen('192.0.2.123', 12345);
    }
    if ($sock) {
        fwrite($sock, json_encode($data, JSON_UNESCAPED_UNICODE) . "\n");
    }
}

192.0.2.123 で nc で待ち受けます。

$ nc -lk 12345

PHP で次のように呼び出します。

<?php
trace(123);
trace(['key' => 123]);

192.0.2.123 のコンソールに垂れ流されます。

$ nc -lk 12345
123
{"key":123}

次のように変更すれば、

<?php
function trace($data)
{
    static $sock;
    if (isset($sock) === false){
        $sock = fsockopen('udp://192.0.2.123', 12345);
    }
    if ($sock) {
        fwrite($sock, json_encode($data, JSON_UNESCAPED_UNICODE) . "\n");
    }
}

UDP でもできます。

$ nc -ukl 12345 -w 0
123
{"key":123}

次のように変更すれば、

<?php
function trace($data)
{
    static $sock;
    if (isset($sock) === false){
        $sock = fsockopen('tls://192.0.2.123', 12345);
    }
    if ($sock) {
        fwrite($sock, json_encode($data, JSON_UNESCAPED_UNICODE) . "\n");
    }
}

TLS でもできます。

$ sudo openssl s_server -key /etc/pki/tls/private/localhost.key -cert /etc/pki/tls/certs/localhost.crt -accept 12345 -quiet
123
{"key":123}
20
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?