/[cvs]/nfo/php/libs/fr.free.lwest/Net/HTTP/net_http_client-en.html
ViewVC logotype

Contents of /nfo/php/libs/fr.free.lwest/Net/HTTP/net_http_client-en.html

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Tue Apr 1 20:11:52 2003 UTC (21 years, 4 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/html
initial commit, visit http://lwest.free.fr/doc/php/lib/index.php3?page=net_http_client&lang=fr

1 <html><head><meta name="keywords" content="php php4 http webdav client file fetching">
2 <meta name="language" content="English">
3 <meta name="author" content="Léo WEST">
4 <style type="text/css">
5
6 body,td,th,p,li,a {
7 font-family: Verdana, Arial, Helvetica;
8 font-size: 11px;
9 }
10
11 body {
12 background-color: #ffffff;
13 margin-top: 2em;
14 margin-bottom: 2em;
15 }
16
17 A {
18 color: #06a;
19 text-decoration: none;
20 }
21
22 A:hover {
23 color: red;
24
25 }
26
27
28 p,ul,li,table {
29 margin-left: 3em;
30 }
31
32 p.note {
33 background-color: #fefecc;
34 font-size: smaller;
35 padding: 8px;
36 }
37
38 span.code {
39 color: #448; font-weight: bold;
40 }
41
42 div.code {
43 border : 1px solid #88a;
44 font-family: Courier, Fixed, Arial;
45 font-size: smaller;
46 color: green;
47 border: 1px solid marron;
48 padding: 1em;
49 margin-top: 1em;
50 margin-bottom: 5em;
51 margin-right: 3em;
52 margin-left: 4em;
53 background-color: #f0eeee;
54 }
55
56
57 ul {
58 list-style-type: none;
59 list-style-position: outside;
60 }
61
62 li {
63 padding: 1px 0px 1px 0px;
64 margin-left: 2px;
65 }
66
67 h1,h2,h3,h4,h5,h6,h7 {
68 font-family: Verdana, Arial, Helvetica, Sans-serif;
69 color: maroon;
70 }
71
72 h2 { margin-left: 1em; }
73 h3 { margin-left: 2em; }
74 h4 { margin-left: 3em; }
75
76 h5,h6 {
77 margin-left: 4em;
78 font-weight: bold;
79 text-decoration: underline;
80 }
81
82 h1 {
83 text-align: center;
84 font-variant: small-caps;
85 }
86
87
88 .aIdx {
89 color: blue;
90 font-weight: bold;
91 text-decoration: none;
92 vlink-color: darkblue;
93
94 }
95 .aIdx:active { color: pink; }
96 .aIdx:hover { color: pink; }
97
98 table.params {
99 border-collapse: separate;
100 padding : 2 2 1 1;
101 }
102
103 th {
104 vertical-align: top;
105 text-align: center;
106 }
107
108 td {
109 font-family: Arial, Helvetica, Sans-serif;
110 }
111 </style>
112
113 </head>
114 <body>
115 <a name="sec_1"><h1> HTTP Client class</h1></a>
116 <p class="text"> Net_HTTP_Client is an almost complete HTTP Client, implementing all <strong>HTTP</strong> methods and a subset of <strong>WebDAV</strong> the standard web publishing protocol.<br>
117 <br>
118 <a href="zip/httpclient.zip">Download httpclient.zip</a>, a zip archive providing the class and documentation.<br>
119 <br>
120 </p><a name="sec_2"><h3> Informations</h3></a>
121 <p class="text"> Version : 0.7<br>
122 Licence : GPL - You may send a mail if you enjoy this component<br>
123 Lastmod : Aug 2001<br>
124 Author : <a href="mailto:west_leo@yahoo-REMOVE-.com?subject=HttpClient">Leo West</a><br>
125 <br>
126 </p><a name="sec_3"><h3> Documentation</h3></a>
127
128 <ul>
129 <ul>
130 <ul>
131 <li> <a class="index" href="#sec_4">Sample use</a>
132 <ul>
133 <li> <a class="index" href="#sec_5">A minimal example</a>
134 <li> <a class="index" href="#sec_6">Using HTTP/1.1 persistent connexions</a>
135 </ul>
136 <li> <a class="index" href="#sec_7">Main methods</a>
137 <ul>
138 <li> <a class="index" href="#sec_8">Net_HTTP_Client( [host, port] )</a>
139 <li> <a class="index" href="#sec_9">Connect( host, port )</a>
140 <li> <a class="index" href="#sec_10">Disconnect()</a>
141 <li> <a class="index" href="#sec_11">setHeaders( headers )</a>
142 <li> <a class="index" href="#sec_12">removeHeader( headerName )</a>
143 <li> <a class="index" href="#sec_13">setCredentials( username, password )</a>
144 <li> <a class="index" href="#sec_14">setProtocolVersion( version )</a>
145 <li> <a class="index" href="#sec_15">setProxy( proxyHost, proxyPort )</a>
146 <li> <a class="index" href="#sec_16">addHeader( headerName, headerValue )</a>
147 <li> <a class="index" href="#sec_16a">addCookie( cookieName, cookieValue )</a>
148 <li> <a class="index" href="#sec_16b">removeCookies()</a>
149 <li> <a class="index" href="#sec_17">Get( uri )</a>
150 <li> <a class="index" href="#sec_18">getHeaders()</a>
151 <li> <a class="index" href="#sec_19">getBody()</a>
152 <li> <a class="index" href="#sec_20">Head( uri )</a>
153 <li> <a class="index" href="#sec_20a">Options( uri )</a>
154 <li> <a class="index" href="#sec_21">Post( uri, query_params )</a>
155 </ul>
156 <li> <a class="index" href="#sec_22">WebDAV methods</a>
157 <ul>
158 <li> <a class="index" href="#sec_23">Copy( srcUri, destUri, overwrite )</a>
159 <li> <a class="index" href="#sec_24">Delete( uri )</a>
160 <li> <a class="index" href="#sec_25">Move( srcUri, destUri, overwrite )</a>
161 <li> <a class="index" href="#sec_26">MkCol( uri )</a>
162 <li> <a class="index" href="#sec_27">PropFind( uri, scope )</a>
163 <li> <a class="index" href="#sec_28">Put( uri, filecontent )</a>
164 <li> <a class="index" href="#sec_29">Lock( uri, lockScope, lockType, lockOwner )</a>
165 <li> <a class="index" href="#sec_30">Unlock( uri, lockToken )</a>
166 </ul>
167 <li> <a class="index" href="#sec_31">Diagnostics and debug</a>
168 <ul>
169 <li> <a class="index" href="#sec_32">getStatus()</a>
170 <li> <a class="index" href="#sec_33">getStatusMessage()</a>
171 <li> <a class="index" href="#sec_34">setDebug( flags )</a>
172 <li> <a class="index" href="#sec_35">Other methods</a>
173 </ul>
174 <li> <a class="index" href="#sec_36">Changelog</a>
175 <li> <a class="index" href="#sec_37">References</a>
176
177 </ul>
178 </ul>
179 </ul>
180 <a name="sec_4"><h3> Sample use</h3></a>
181 <a name="sec_5"><h4> A minimal example</h4></a>
182 <div class="code"><p class="text"> include &quot;Net/HTTP/Client.php&quot;;<br>
183 <br>
184 $http = new Net_HTTP_Client();<br>
185 $http-&gt;Connect( &quot;somehost&quot;, 80 ) or die( &quot;Connect problem&quot; );<br>
186 $status = $http-&gt;Get( &quot;/index.html&quot; );<br>
187 if( $status != 200 )<br>
188 die( &quot;Problem : &quot; . $http-&gt;getStatusMessage() );<br>
189 else<br>
190 echo $http-&gt;getBody();<br>
191 $http-&gt;Disconnect();<br>
192 </div>
193 <br>
194 </p><a name="sec_6"><h4> Using HTTP/1.1 persistent connexions</h4></a>
195 <div class="code"><p class="text"> $http = new Net_HTTP_Client( &quot;dir.yahoo.com&quot;, 80 );<br>
196 $http-&gt;setProtocolVersion( &quot;1.1&quot; );<br>
197 $http-&gt;addHeader( &quot;Host&quot;, &quot;dir.yahoo.com&quot; );<br>
198 $http-&gt;addHeader( &quot;Connection&quot;, &quot;keep-alive&quot; );<br>
199 <br>
200 if( $http-&gt;Get( &quot;/Reference/Libraries/&quot; ) == 200 )<br>
201 $page1 = $http-&gt;getBody();<br>
202 <br>
203 if( $http-&gt;Get( &quot;/News_and_Media/&quot; ) == 200 )<br>
204 $page2 = $http-&gt;getBody();<br>
205 $http-&gt;disconnect();<br>
206 </div>
207 <br>
208 </p><a name="sec_7"><h3> Main methods</h3></a>
209 <a name="sec_8"><h4> Net_HTTP_Client( [host, port] )</h4></a>
210 <p class="text"> Class constructor<br>
211 parameters host and port are optional, but when defined, the connection is immediate.<br>
212 <strong>seeAlso</strong> : Connect method<br>
213 <br>
214 </p><a name="sec_9"><h4> Connect( host, port )</h4></a>
215 <p class="text"> Open the connection to the server<br>
216 <span class="code">host</span> is the server address (or IP)<br>
217 <span class="code">port</span> is the optional server listening port, defaults to 80<br>
218 return true if successfull, false is connection failed. Use getStatusMessage to examine the error problem<br>
219 <br>
220 </p><a name="sec_10"><h4> Disconnect()</h4></a>
221 <p class="text"> close the connection to the server<br>
222 <br>
223 </p><a name="sec_11"><h4> setHeaders( headers )</h4></a>
224 <p class="text"> Define all HTTP headers to be sent during next requests.<br>
225 <span class="code">headers</span> is an array containing the headers in the form "headerName" => "headerValue"<br>
226 header names are case sensitive in this class.<br>
227 <br>
228 </p><a name="sec_12"><h4> removeHeader( headerName )</h4></a>
229 <p class="text"> unset a request header<br>
230 <span class="code">headerName</span> is the header name. Be aware i choose to use <strong>case sensitive</strong> headers.<br>
231 <br>
232 </p><a name="sec_13"><h4> setCredentials( username, password )</h4></a>
233 <p class="text"> Set the username and password to access a protected resource on the server.<br>
234 Only &quot;Basic&quot; authentication scheme is supported yet<br>
235 <br>
236 <span class="code">username </span> The user identifier<br>
237 <span class="code">password </span> The user password (clear form)<br>
238 <br>
239 </p><a name="sec_14"><h4> setProtocolVersion( version )</h4></a>
240 <p class="text"> Define the HTTP protocol version to use<br>
241 <span class="code">version</span> is a string representing the version number, with one digit: "0.9", "1.0", "1.1"<br>
242 return false if the version number is bad, true if OK<br>
243 <br>
244 Note that for some obscure reasons, persistent connexions sometimes fail with some versions of PHP (4.0.6 notably)<br>
245 Socket gurus, you may track down the problem in the processBody() code.<br>
246 <br>
247 </p><a name="sec_15"><h4> setProxy( proxyHost, proxyPort )</h4></a>
248 <p class="text"> Instruct the class to use a connect through a proxy. Tested only against HTTP proxies (Squid etc.)<br>
249 <br>
250 <span class="code">proxyHost</span> is the proxy address, <span class="code">proxyPort</span> the proxy port, usually 80 or 8080<br>
251 <br>
252 </p><a name="sec_16"><h4> addHeader( headerName, headerValue )</h4></a>
253 <p class="text"> Define a request header to be sent during next requests. headerName is case sensitive<br>
254 <span class="code">headerName</span> HTTP header name<br>
255 <span class="code">headerValue</span> HTTP header value<br>
256 <br>
257 </p><a name="sec_16a"><h4> addCookie( cookieName, cookieValue )</h4></a>
258 <p class="text"> set a cookie to use for the next requests<br>cookie is a session-type cookie.
259
260 <span class="code">cookieName</span> cookie name<br>
261 <span class="code">cookieValue</span> cookie value<br>
262 <small>[since v0.7]</small>
263 <br>
264 </p><a name="sec_16b"><h4> removeCookies( )</h4></a>
265 <p class="text"> discards all defined cookies<br>
266 <small>[since v0.7]</small>
267 <br>
268 </p><a name="sec_17"><h4> Get( uri )</h4></a>
269 <p class="text"> issue a GET http request<br>
270 <span class="code">$uri</span> is the URI of the document<br>
271 <span class="code">Returns</span> the status code received from server (200 if ok)<br>
272 see Also getHeaders &amp; getBody methods<br>
273 <br>
274 </p><a name="sec_18"><h4> getHeaders()</h4></a>
275 <p class="text"> return the response headers. Headers are returned as an (headername =&gt; value) array.<br>
276 To be called after a request, to examine the headers returned by server: Set-Cookie, Location or whatever.<br>
277 <div class="code"> $status = $http-&gt;Get( &quot;/&quot; );<br>
278 ...<br>
279 // document is somewhere else<br>
280 if( $status == 301 || $status == 302 || $status == 307 )<br>
281 {<br>
282 $headers = $http-&gt;getHeaders();<br>
283 $status = $http-&gt;Get( $headers[&quot;Location&quot;] );<br>
284 }<br>
285 <br>
286 </div>
287 <br>
288 </p><a name="sec_19"><h4> getBody()</h4></a>
289 <p class="text"> return a string containing the response body, to be used after a Get or Post call for instance.<br>
290 <br>
291 </p><a name="sec_20"><h4> Head( uri )</h4></a>
292 <p class="text"> issue a HEAD request<br>
293 $uri is a string containing the URI of the document ( the part of the URL after the host and port /)<br>
294 <strong>Returns</strong> the status code received from server (200 if ok)<br>
295 To examine the headers content, see getResponseHeaders method<br>
296 <br>
297 </p><a name="sec_20a"><h4> Options( uri )</h4></a>
298 <p class="text"> issue an OPTIONS request<br>
299 <span class="code">uri</span> document URI, usually "/"<br>
300 Returns an array with the options supported by the server<br>
301 <small>[since v0.7]</small>
302 <br>
303 </p><a name="sec_21"><h4> Post( uri, query_params )</h4></a>
304 <p class="text"> issue a POST http request<br>
305 <span class="code">uri</span> is the URI of the document<br>
306 <span class="code">query_params</span> is an hash array containg the form parameters to send<br>
307 Returns the status code received from server (200 if ok)<br>
308 <br>
309 <strong>Example</strong><br>
310 <div class="code"> $params = array( &quot;login&quot; =&gt; &quot;scott&quot;, &quot;password&quot; =&gt; &quot;tiger&quot; );<br>
311 $status = $http-&gt;post( &quot;/login.php&quot;, $params );<br>
312 </div>
313 <br>
314 </p><a name="sec_22"><h3> WebDAV methods</h3></a>
315 <a name="sec_23"><h4> Copy( srcUri, destUri, overwrite )</h4></a>
316 <p class="text"> Copy an existing file on the server into a new place, using the COPY request<br>
317 <br>
318 <span class="code">srcUri</span> is the current file location on the server. dont forget the heading "/"<br>
319 <span class="code">destUri</span> is the destination location on the server. this is *not* a full URL<br>
320 <span class="code">overwrite</span> indicates whether to overwrite (true) or leave (false) an existing destination. overwrite by default<br>
321 Returns the status code 204 (Unchanged) if OK.<br>
322 <br>
323 <span class="code">$status = $http-&gt;Copy( "/sources/client.php", "/backup/client-0.4.php", true );</span><br>
324 <br>
325 </p><a name="sec_24"><h4> Delete( uri )</h4></a>
326 <p class="text"> Delete a file on the server using the &quot;DELETE&quot; HTTP-DAV request<br>
327 <span class="code">uri</span> the location of the file on the server.<br>
328 Returns the status code (204 if OK)<br>
329 <br>
330 Note: This HTTP method is *not* widely supported, and only partially supports &quot;collection&quot;<br>
331 deletion, as the XML response is not parsed.<br>
332 <br>
333 </p><a name="sec_25"><h4> Move( srcUri, destUri, overwrite )</h4></a>
334 <p class="text"> Move or rename a resource on the server, using the MOVE request.<br>
335 <span class="code">srcUri</span> is the current file location on the server. dont forget the heading /<br>
336 <span class="code">destUri</span> the destination location on the server. this is *not* a full URL<br>
337 <span class="code">overwrite</span> is a boolean set to true to overwrite an existing destination.<br>
338 Return the response status code as a string, 204 (Unchanged) if OK.<br>
339 <br>
340 </p><a name="sec_26"><h4> MkCol( uri )</h4></a>
341 <p class="text"> Send a MKCOL HTTP-DAV request<br>
342 Create a collection (usually a directory) on the server.<br>
343 <span class="code">uri</span> is the collection's location on the server.<br>
344 Return the status code, 201 (Created) if OK<br>
345 <br>
346 </p><a name="sec_27"><h4> PropFind( uri, scope )</h4></a>
347 <p class="text"> Retrieves meta informations about a resource on the server, using the WebDAV PROPFIND method<br>
348 The XML reply body is <strong>not parsed</strong>, therfore you will have to do it :)<br>
349 <br>
350 <span class="code">uri</span> the location of the file on the server<br>
351 <span class="code">scope</span> set the scope of the request, (somehow similar to the LDAP scope notion):<br>
352 To retrieve infos about the node only (0), for the node and its direct children (1), or &quot;Infinity&quot; infos for the node and all its descendant nodes.<br>
353 <br>
354 Returns the response status code, 207 (Multi-Status) if OK<br>
355 <br>
356 </p><a name="sec_28"><h4> Put( uri, filecontent )</h4></a>
357 <p class="text"> Send a PUT request<br>
358 PUT is the method to sending a file on the server. it is *not* widely supported<br>
359 <span class="code">uri</span> the location of the file on the server. dont forget the heading "/"<br>
360 <span class="code">filecontent</span> is the content of the file. binary content accepted<br>
361 Returns the status code as a string, 201 (Created) if OK.<br>
362 <br>
363 </p><a name="sec_29"><h4> Lock( uri, lockScope, lockType, lockOwner )</h4></a>
364 <p class="text"> <span class="note">WARNING: experimental</span><br>
365 Lock a ressource on the server and return 200 if locking went OK.<br>
366 The XML reply is not parsed for now [translate: Your help is welcome].<br>
367 <br>
368 <span class="code">uri </span> URL of the resource to lock<br>
369 <span class="code">lockScope </span> scope of the lock. Use "exclusive" for an private lock, "inclusive" for a shared lock.<br>
370 <span class="code">lockType </span> Access type for the lock : Use "write", ?<br>
371 <span class="code">lockOwner </span> An URL representing the owner of this lock<br>
372 <br>
373 <div class="code"> $reply = $http-&gt;Lock( &quot;/docs/manual.html&quot;, &quot;inclusive&quot;, &quot;write&quot;, &quot;leo@leo.com&quot; );<br>
374 if( $reply != 200 ) {<br>
375 $msg = $http-&gt;getStatusMessage();<br>
376 die( &quot;Unlock problem, server says $msg&quot; );<br>
377 } else {<br>
378 $xmlResponse = $http-&gt;getBody();<br>
379 // TODO : parse the XML response here<br>
380 }<br>
381 </div>
382 <br>
383 </p><a name="sec_30"><h4> Unlock( uri, lockToken )</h4></a>
384 <p class="text"> <span class="note">WARNING: experimental</span><br>
385 Release a lock on a remote file and return the response code (204 if unlock OK)<br>
386 <span class="code">uri</span> The relative URL of the resource to unlock<br>
387 <span class="code">lockToken</span> The lock token given at lock time, eg: opaquelocktoken:e71d4fae-5dec-22d6-fea5-00a0c91e6be4<br>
388 <br>
389 <div class="code"> $token = &quot;opaquelocktoken:e71d4fae-5dec-22d6-fea5-00a0c91e6be4&quot;;<br>
390 $reply = $http-&gt;Unlock( &quot;/docs/manual.html&quot;, $token );<br>
391 if( $reply != 204 ) {<br>
392 $msg = $http-&gt;getStatusMessage();<br>
393 die( &quot;Unlock problem, server says $msg&quot; );<br>
394 }<br>
395 </div>
396 <br>
397 </p><a name="sec_31"><h3> Diagnostics and debug</h3></a>
398 <a name="sec_32"><h4> getStatus()</h4></a>
399 <p class="text"> Return the server status code for the last request.<br>
400 HTTP codes are divided in classes (where x is a digit)<br>
401 <br>
402 - 20x : request processed OK<br>
403 - 30x : document moved<br>
404 - 40x : client error ( bad url, document not found, etc...)<br>
405 - 50x : server error<br>
406 <br>
407 </p><a name="sec_33"><h4> getStatusMessage()</h4></a>
408 <p class="text"> Returns the full response status of the last request, in the form &quot;CODE Message&quot;<br>
409 Example : <span class="code">404 Document not found</span><br>
410 <br>
411 </p><a name="sec_34"><h4> setDebug( flags )</h4></a>
412 <p class="text"> Turn on debug messages<br>
413 <span class="code">$client-&gt;setDebug( DBGTRACE | DBGINDATA );</span><br>
414 <br>
415 Flags is a bit mask of following debug modes:<br>
416 </p><!-- table --><table class="params">
417 <tr> <td class="params">DBGTRACE</td>
418 <td class="params">to trace methods calls stack</td>
419 </tr>
420 <tr> <td class="params">DBGINDATA</td>
421 <td class="params">to debug data received from server</td>
422 </tr>
423 <tr> <td class="params">DBGOUTDATA</td>
424 <td class="params">to debug data sent</td>
425 </tr>
426 <tr> <td class="params">DBGLOW</td>
427 <td class="params">to debug low-level (usually internal) methods</td>
428 </tr>
429 <tr> <td class="params">DBGSOCK</td>
430 <td class="params">to debug socket-level stuff</td>
431 </tr>
432 </table><a name="sec_35"><h4> Other methods</h4></a>
433 <p class="text"> sendCommand, makeURI, processReply, processHeader, processBody, readReply, flushReply<br>
434 These methods are private therefore not documented and USUALLY NOT FOR DIRECT USE.<br>
435 <br>
436 </p><a name="sec_36"><h3> Changelog</h3></a>
437 <p class="text"><pre>
438 0.1 initial version
439 0.2 documentation completed
440 + getHeaders(), getBody()
441 o Post(), Connect()
442 0.3 DAV enhancements:
443 + Put() method
444 0.4 continued DAV support
445 + Delete(), Move(), MkCol(), Propfind() methods
446 o added url property, remove host and port properties
447 o Connect, Net_HTTP_Client (use of this.url)
448 o processBody() : use non-blocking to fix a socket pblm
449 0.5 debug support
450 + setDebug()
451 + debug levels definitions (DBG*)
452 0.6 + Lock() method
453 + setCredentials() method and fix - thanks Thomas Olsen
454 + support for Get( full_url )
455 o fix POST call (duplicate content-length) - thanks to Javier Sixto
456 0.7 + OPTIONS method support
457 + addCookie and removeCookies methods
458 o fix the "0" problem
459 o fix undefined variable warning
460 </pre>
461 </p><a name="sec_37"><h3> References</h3></a>
462 <p class="text"> RFC2616 &quot;Hypertext Transfer Protocol -- HTTP/1.1&quot;<br>
463 <a href="http://lwest.free.fr/doc/protocols/http/rfc2616.html" target="_new">http://lwest.free.fr/doc/protocols/http/rfc2616.html</a><br>
464 <br>
465 RFC2518 &quot;HTTP Extensions for Distributed Authoring WEBDAV&quot;<br>
466 <a href="http://lwest.free.fr/doc/protocols/http/rfc2518.html" target="_new">http://lwest.free.fr/doc/protocols/http/rfc2518.html</a><br>
467 <br>
468 WebDAV Community Site<br>
469 <a href="http://www.webdav.org" target="_new">http://www.webdav.org</a><br>
470 <br>
471 </body>
472 </html>
473

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed