[PHP]GuzzleHttpで通信の内容をデバッグ出力する

カテゴリ: GuzzleHttp

PHPの通信ライブラリであるGuzzleHttpで通信の送受信内容をデバッグしたい場合があります。

デバッグ出力するためには、get()やpost()など通信実行のメソッドの第二引数に、'debug'=>trueのパラメータを追加すればよいです。

<?php
require "./vendor/autoload.php";

$client = new \GuzzleHttp\Client();

$url = 'http://www.yahoo.co.jp/';
$response = $client->post($url, ['debug' => true]);

このコードの実行結果は以下のような感じで、HTTPの通信内容が標準出力にダンプされます。

*   Trying 182.22.24.124...
* TCP_NODELAY set
* Connected to www.yahoo.co.jp (182.22.24.124) port 80 (#0)
> POST / HTTP/1.1
Host: www.yahoo.co.jp
Content-Length: 0
User-Agent: GuzzleHttp/6.2.1 curl/7.51.0 PHP/7.0.18

< HTTP/1.1 301 Redirect
< Date: Tue, 04 Jul 2017 05:12:17 GMT
< Connection: keep-alive
< Via: http/1.1 edge1206.img.bbt.yahoo.co.jp (ApacheTrafficServer [c s f ])
< Server: ATS
< Cache-Control: no-store
< Location: https://www.yahoo.co.jp:443/
< Content-Type: text/html
< Content-Language: en
< Content-Length: 6794
<
* Curl_http_done: called premature == 0
* Connection #0 to host www.yahoo.co.jp left intact
*   Trying 182.22.24.124...
* TCP_NODELAY set
* Connected to www.yahoo.co.jp (182.22.24.124) port 443 (#1)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* Curl_http_done: called premature == 1
* Closing connection 1

Amazonでおトクに買い物する方法
AmazonチャージでポイントGET


Amazonは買いもの前にAmazonギフト券をチャージしてポイントをゲットしないと損!

こちらもおススメ

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です