9ca4ba8fc1de025c8b54427c84e7fc49c5498eed
[mdref/mdref-http] / http / Client / setDebug.md
1 # http\Client http\Client::setDebug(callable $callback)
2
3 Set client debugging callback.
4
5 > ***NOTE:***
6 > This method has been added in v2.6.0, resp. v3.1.0.
7
8 ## Params:
9
10 * callable $callback as function(http\Client $c, http\Client\Request $r, int $type, string $data)
11 The debug callback. For $type see http\Client::DEBUG_* constants.
12
13 ## Returns:
14
15 * http\Client, self.
16
17 ## Throws:
18
19 * http\Exception\InvalidArgumentException
20
21 ## Example:
22
23 <?php
24
25 (new http\Client)->setDebug(function($c, $r, $type, $data) {
26 if ($type === http\Client::DEBUG_INFO) {
27 printf("INFO: %s\n", $data);
28 } else {
29 repeat:
30 if ($type & http\Client::DEBUG_OUT) {
31 $sep = ">";
32 } elseif ($type & http\Client::DEBUG_IN) {
33 $sep = "<";
34 }
35 if ($type & http\Client::DEBUG_SSL) {
36 printf("%s <%d bytes of SSL data>\n", $sep, strlen($data));
37 } else {
38 printf("%s %s\n", $sep, str_replace("\n", "\n$sep ", rtrim($data, "\n")));
39 }
40 }
41 })->enqueue(new http\Client\Request("GET", "http://example.com"))->send();
42
43 ?>
44
45 > ***NOTE:***
46 > Except for http\Client::DEBUG_INFO, which always occurs separately, the debug
47 > callback's $type argument contains a bitmask of (http\Client::DEBUG_IN or http\Client::DEBUG_OUT)
48 > and (http\Client::DEBUG_HEADER or http\Client::DEBUG_BODY or http\Client::DEBUG_SSL).
49
50 ### Yields:
51
52 INFO: Trying 93.184.216.34...
53
54 INFO: Connected to example.com (93.184.216.34) port 80 (#0)
55
56 > GET / HTTP/1.1
57 > Host: example.com
58 > User-Agent: PECL_HTTP/2.6.0dev PHP/5.6.19RC1 libcurl/7.50.0-DEV
59 > Accept: */*
60 >
61 < HTTP/1.1 200 OK
62 < Cache-Control: max-age=604800
63 < Content-Type: text/html
64 < Date: Thu, 18 Aug 2016 17:55:10 GMT
65 < Etag: "359670651+gzip+ident"
66 < Expires: Thu, 25 Aug 2016 17:55:10 GMT
67 < Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
68 < Server: ECS (iad/182A)
69 < Vary: Accept-Encoding
70 < X-Cache: HIT
71 < x-ec-custom-error: 1
72 < Content-Length: 1270
73 <
74 < <!doctype html>
75 < <html>
76 < <head>
77 < <title>Example Domain</title>
78 <
79 < <meta charset="utf-8" />
80 < <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
81 < <meta name="viewport" content="width=device-width, initial-scale=1" />
82 < <style type="text/css">
83 < body {
84 < background-color: #f0f0f2;
85 < margin: 0;
86 < padding: 0;
87 < font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
88 <
89 < }
90 < div {
91 < width: 600px;
92 < margin: 5em auto;
93 < padding: 50px;
94 < background-color: #fff;
95 < border-radius: 1em;
96 < }
97 < a:link, a:visited {
98 < color: #38488f;
99 < text-decoration: none;
100 < }
101 < @media (max-width: 700px) {
102 < body {
103 < background-color: #fff;
104 < }
105 < div {
106 < width: auto;
107 < margin: 0 auto;
108 < border-radius: 0;
109 < padding: 1em;
110 < }
111 < }
112 < </style>
113 < </head>
114 <
115 < <body>
116 < <div>
117 < <h1>Example Domain</h1>
118 < <p>This domain is established to be used for illustrative examples in documents. You may use this
119 < domain in examples without prior coordination or asking for permission.</p>
120 < <p><a href="http://www.iana.org/domains/example">More information...</a></p>
121 < </div>
122 < </body>
123 < </html>
124 INFO: Connection #0 to host example.com left intact