Using the Client

HTTP_Client persists explicit sets of headers and requests parameters across multiple requests, which are set using the setDefaultHeader and setRequestParameter methods respectively. The client constructor also accepts arrays of these. Default headers and request parameters can be cleared by calling reset on the client instance.

Internally, the client class actually creates a new instance of HTTP_Request per request. The request operation is set depending on which of the client instance methods are called; get, head, and post are supported.

The capabilities of the client described up to this point can all be accomplished by reusing the same request instance for multiple requests. However, the client also handles two things that the request class does not: cookies and redirects.

By default, cookies are persisted automatically across requests without any additional configuration. HTTP_Client_CookieManager is used internally for this. For custom cookie handling, this class can be extended and an instance of it passed as the third parameter to the client constructor. If this is done, that instance will be used rather than an instance of the native cookie manager class being created by default.

The maximum number of redirects to process can be set using the setMaxRed i rects method of the client class. Internally, requests will be created and sent as needed to process the redirect until a non-redirecting response is received or the maximum redirect limit is reached. In the former case, the client method being called will return an integer containing the response code rather than true as the request class does. In the latter case, the client method will return an error instance. Note that the client class will process redirects contained in meta tags of HTML documents in addition to those performed at the HTTP level.

To retrieve information for the last response received, use the currentResponse method of the client instance. It will return an associative array containing the keys 'code ’, 'headers ’, and 'body’with values corresponding to the return values of request methods getResponseCode, getResponseHeader, and getResponseBody respectively.

By default, all responses are stored and can be accessed individually as shown below. To disable storage of all responses except the last one, call enableHisto ry on the client instance and pass it false.

<?php
for ($client->rewind();$client->valid(); $client->next()) {
$url = $client->key();
$response = $client->current();
}
?>

© PEAR::HTTP_Client — Web Scraping

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (01.09.2014)
Views: 281 | Rating: 0.0/0
Total comments: 0
avatar