Class User_Agent

Description

User Agent class

This class is based on UserAgent.pm <http://search.cpan.org/author/GAAS/libwww-perl-5.65/lib/LWP/UserAgent.pm> from the libwww-perl collection <http://www.linpro.no/lwp/>. A lot of the documentation here has been copied from the perl files.

The User_Agent is a class implementing a HTTP user agent in PHP. It brings together the HTTP_Request, HTTP_Response and the Protocol classes that form the rest of the core of HTTP Navigator library. For simple uses this class can be used directly to dispatch WWW requests, alternatively it can be subclassed for application-specific behaviour.

In normal use the application creates a User_Agent object, and then configures it with values for timeouts, proxies, name, etc. It then creates an instance of HTTP_Request for the request that needs to be performed. This request is then passed to one of the User_Agent's request() methods, which dispatches it using the relevant protocol, and returns a HTTP_Response object.

There are convenience methods for sending the most common request types; get(), head() and post().

Example:


1 $ua =& new User_Agent(array('timeout' => 30, 'cookie_jar' => true));
2 $response =& $ua->get('http://www.k1m.com/');
or:

1 $request =& new HTTP_Request('GET', new URL('http://www.k1m.com/'));
2 // then one of these:
3 $response =& $ua->request($request); // or
4 $response =& $ua->request($request, new File('/tmp/sss')); // or
5 $response =& $ua->request($request, new Callback('function'));

TODO:

  • head parser

Located in Program_Root/User_Agent.php (line 75)


	
			
Variable Summary
Method Summary
void User_Agent ([mixed $options = null], [mixed $override = null])
void destroy ()
object &get (mixed $url, [array $headers = null])
string get_agent ()
void get_basic_credentials (mixed $realm, mixed $url, [mixed $proxy = false])
object reference &get_connection_cache ()
object reference &get_cookie_jar ()
string get_from ()
string get_http_version ()
int get_max_size ()
bool get_parse_head ()
object URL get_proxy (string $scheme)
int get_timeout ()
object &head (mixed $url, [array $headers = null])
bool is_redirect_ok (object &$request)
void parse_html_head (mixed &$response)
object &post (mixed $url, [array $form_data = null], [array $headers = null])
bool prepare_request (object &$request)
object &put (mixed $url, string $content, [array $headers = null])
void &request (mixed &$request, [mixed $arg = null], [mixed $previous = null])
void &send_request (object &$request, [mixed $arg = null])
void set_agent (string $agent)
void set_connection_cache (object &$connection_cache)
void set_cookie_jar (object $cookie_jar)
void set_credentials (mixed $netloc, string $realm, string $user, [string $pass = ''])
void set_from (string $from)
bool set_gzip_support (mixed $enable, bool $val)
void set_http_version (string $ver)
void set_keep_alive (int $keep_alive)
void set_lax_redirect (bool $lax)
void set_max_size (int $max_size)
void set_parse_head (bool $parse_head)
void set_protocols_allowed (array $protocols_allowed)
void set_protocols_forbidden (array $protocols_forbidden)
void set_proxy (mixed $schemes, mixed $url)
void set_requests_redirectable (array $requests_redirectable)
void set_scheme_implementor (array $array)
void set_timeout (int $timeout)
void set_timeout_rw (int $timeout)
object false &simple_request (object &$request, [mixed $arg = null])
void uncompress_content (object &$response)
Variables
string $agent = HTTPNAV_USER_AGENT (line 81)

Agent - sent as a 'User-Agent' header

array $auth = array() (line 171)

Auth - basic auth details

object $connection_cache (line 129)

Connection cache

object $cookie_jar (line 123)

Cookie jar

string $from (line 87)

From - email address

bool $gzip_support = false (line 177)

Gzip support

string $http_version = '1.1' (line 93)

HTTP version

int $keep_alive (line 153)

Keep alive (total number of connections to keep alive)

bool $lax_redirect = false (line 183)

Lax redirect

int $max_size (line 117)

Max content size

array $no_proxy = array() (line 165)

Exclude from proxy - requests to these domains will not go through a proxy

bool $parse_head = false (line 111)

Parse HTML head

array $protocols_allowed (line 135)

Protocols allowed

array $protocols_forbidden (line 141)

Protocols forbidden

array $proxy = array() (line 159)

Proxy details

array $requests_redirectable = array('GET', 'HEAD') (line 147)

Requests Redirectable

int $timeout = 180 (line 99)

Timeout (in seconds)

int $timeout_rw = 60 (line 105)

Read / write timeout (seconds)

Methods
Constructor User_Agent (line 212)

Constructor.

Key/value pair arguments may be provided to set up the initial state of the user agent. The following options correspond to attribute methods described below:

    KEY                     DEFAULT
    -----------             --------------------
    agent                   "HTTP-Navigator/#.##"
    from                    null
    timeout                 180
    max_size                null
    http_version            '1.1'
    cookie_jar              null
    lax_redirect            false
    parse_head              false
    connection_cache        null
    protocols_allowed       null
    protocols_forbidden     null
    gzip_support            false
    scheme_implementor      null
    requests_redirectable   ('GET', 'HEAD')
 

void User_Agent ([mixed $options = null], [mixed $override = null])
  • mixed $options:

    string or array

  • mixed $override:

    string or array

destroy (line 1133)

Destroy - call this when you've finished with the User Agent

void destroy ()
get (line 1028)

GET

object &get (mixed $url, [array $headers = null])
  • mixed $url:

    URL object, or string

  • array $headers:

    HTTP_Headers object

get_agent (line 257)

Get agent

string get_agent ()
get_basic_credentials (line 1119)
void get_basic_credentials (mixed $realm, mixed $url, [mixed $proxy = false])
get_connection_cache (line 503)

Get connection_cache

object reference &get_connection_cache ()
get_cookie_jar (line 482)

Get cookie jar

Example:


1 $cookie_jar =& $ua->get_cookie_jar();

object reference &get_cookie_jar ()
get_from (line 275)

Get from

string get_from ()
get_gzip_support (line 695)

Get Gzip support

bool get_gzip_support ()
get_http_version (line 293)

Get http version

string get_http_version ()
get_keep_alive (line 603)

Get keep_alive

int get_keep_alive ()
get_lax_redirect (line 382)

Get lax redirect

bool get_lax_redirect ()
get_max_size (line 412)

Get maximum content size

int get_max_size ()
get_parse_head (line 354)

Get parse_head

bool get_parse_head ()
get_protocols_allowed (line 529)

Get protocols_allowed

  • return:

    false if unset

array get_protocols_allowed ()
get_protocols_forbidden (line 554)

Get protocols_forbidden

  • return:

    false if unset

array get_protocols_forbidden ()
get_proxy (line 653)

Get proxy

Example:


1 $proxy = $ua->get_proxy('HTTP');

get_proxy() returns a URL object holding proxy details.

  • return:

    object, or false if scheme has no proxy specified

object URL get_proxy (string $scheme)
  • string $scheme
get_requests_redirectable (line 576)

Get requests_redirectable

  • return:

    false if unset

array get_requests_redirectable ()
get_timeout (line 311)

Get timeout

int get_timeout ()
get_timeout_rw (line 332)

Get timeout read/write

int get_timeout_rw ()
head (line 1040)

HEAD

object &head (mixed $url, [array $headers = null])
  • mixed $url:

    URL object, or string

  • array $headers:

    HTTP_Headers object

is_redirect_ok (line 717)

Is redirect ok.

This method is called by request() before it tries to follow a redirection to the request. This should return a true value if redirection is permissible.

The default implementation will return true unless:

  • The method (e.g. 'GET') is not in the object's requests_redirectable list.
  • The proposed redirection is to a "file://.." URL.
Subclasses might want to override this.

bool is_redirect_ok (object &$request)
  • object $request:

    (passed by reference)

parse_html_head (line 1166)

Parse HTML head

Extracts some headers from the HTML <head> element and adds them to the response headers.

void parse_html_head (mixed &$response)
post (line 1066)

POST

object &post (mixed $url, [array $form_data = null], [array $headers = null])
  • mixed $url:

    URL object, or string

  • array $form_data:

    associative array

  • array $headers:

    HTTP_Headers object

prepare_request (line 840)

Prepare request

This method modifies given HTTP_Request object by setting up various headers based on the attributes of the $ua. The headers affected are: User-Agent, From, Range and Cookie.

bool prepare_request (object &$request)
  • object $request:

    passed by reference

put (line 1053)

PUT

object &put (mixed $url, string $content, [array $headers = null])
  • mixed $url:

    URL object, or string

  • string $content
  • array $headers:

    HTTP_Headers object

request (line 893)

Request

Process a request, including redirects and security. This method may actually send several different simple requests.

The arguments are the same as for send_request() and simple_request().

void &request (mixed &$request, [mixed $arg = null], [mixed $previous = null])
send_request (line 761)

Send request.

This method dispatches a single WWW request on behalf of a user, and returns the response received. The request is sent off unmodified, without passing it through prepare_request().

The $request should be a reference to a HTTP_Request object with values defined for at least the method and url attributes.

If $arg is a File object it is taken as a file where the content of the response is stored.

If $arg is a Callback object, or derived from the Callback class, chunks of the content will be passed to the call() method as they are received.

If $arg is omitted, then the content is stored in the response object itself.

void &send_request (object &$request, [mixed $arg = null])
  • object $request:

    (passed by reference)

  • mixed $arg
set_agent (line 241)

Set agent

void set_agent (string $agent)
  • string $agent
set_connection_cache (line 494)

Set connection_cache

void set_connection_cache (object &$connection_cache)
  • object $connection_cache:

    (passed by reference)

set_cookie_jar (line 454)

Set cookie jar

Get/set the cookie jar object to use. The only requirement is that the cookie jar object must implement the extract_cookies($request) and add_cookie_header($response) methods. These methods will then be invoked by the user agent as requests are sent and responses are received. Normally this will be a Cookie_Jar object or some subclass.

The default is to have no cookie_jar, i.e. never automatically add "Cookie" headers to the requests.

If you pass a string holding the filename of the cookies file, an instance of the Cookie_Jar class will be created holding the cookies found in the file. See examples below for different ways of enabling the cookie jar.

Examples:


1 // setting the cookie jar by file
2 $ua->set_cookie_jar(new File('/path/to/cookies.txt'));
3 // setting the cookie jar by passing a Cookie_Jar object by reference
4 $cookie_jar =& new Cookie_Jar(array('file_persistent' => '/path/to/cookies.txt'));
5 $ua->set_cookie_jar(array(&$cookie_jar));
6 // disabling cookie jar
7 $ua->set_cookie_jar(false);
8 // enabling cookie jar (will create a new empty cookie jar)
9 $ua->set_cookie_jar(true);

void set_cookie_jar (object $cookie_jar)
  • object $cookie_jar:

    (passed by reference)

set_credentials (line 1094)

Set credentials.

It is probably better to extend User_Agent and specialise get_basic_credentials() to look up the auth info however you like.

Example:


1 $ua->set_credentials('www.domain.com:80', 'MySpace', 'user', 'password');
or

1 $url =& new URL('http://www.domain.com');
2 $ua->set_credentials($url, 'MySpace', 'user', 'password');

void set_credentials (mixed $netloc, string $realm, string $user, [string $pass = ''])
  • mixed $netloc:

    URL object, or string

  • string $realm
  • string $user
  • string $pass
set_from (line 266)

Set from

void set_from (string $from)
  • string $from
set_gzip_support (line 676)

Set Gzip support

Returns true if $enable is true and gzinflate() function exists. False otherwise

Example:


1 $ua->set_gzip_support(true);

  • return:

    true if Gzip support has been enabled, false otherwise

bool set_gzip_support (mixed $enable, bool $val)
  • bool $val
set_http_version (line 284)

Set http version

void set_http_version (string $ver)
  • string $ver
set_keep_alive (line 588)

Set keep_alive

void set_keep_alive (int $keep_alive)
  • int $keep_alive
set_lax_redirect (line 373)

Set lax redirect

If enabled, a 302 response will be treated as a 303 response. This is useful if you want a followup request to a 302 response to be handled as a GET request regardless of the original request method.

Note: I'm not sure if this is a good way to handle it, so the effect of lax redirect might change in future.

void set_lax_redirect (bool $lax)
  • bool $lax
set_max_size (line 399)

Set maximum content size.

Pass an integer, or null if you don't want a limit. Get/set the size limit for response content. The default is null, which means that there is no limit. If the returned response content is only partial, because the size limit was exceeded, then a "Client-Aborted" header will be added to the response.

void set_max_size (int $max_size)
  • int $max_size
set_parse_head (line 344)

Set parse_head

void set_parse_head (bool $parse_head)
  • bool $parse_head
set_protocols_allowed (line 515)

Set protocols_allowed.

void set_protocols_allowed (array $protocols_allowed)
  • array $protocols_allowed
set_protocols_forbidden (line 541)

Set protocols_forbidden.

void set_protocols_forbidden (array $protocols_forbidden)
  • array $protocols_forbidden
set_proxy (line 631)

Set proxy

Examples:


1 $ua->set_proxy(array('http', 'ftp'), 'http://proxy.sn.no:8001/');
2 $ua->set_proxy('gopher', 'http://proxy.sn.no:8001/');

The first form specifies that the URL is to be used for proxying of access methods listed in the list in the first method argument, i.e. 'http' and 'ftp'.

The second form shows a shorthand form for specifying proxy URL for a single access scheme.

void set_proxy (mixed $schemes, mixed $url)
  • mixed $schemes:

    single scheme can be specified as a string, multiple schemes in an array.

  • mixed $url:

    URL object or URL string.

set_requests_redirectable (line 566)

Set requests_redirectable.

void set_requests_redirectable (array $requests_redirectable)
  • array $requests_redirectable
set_scheme_implementor (line 1153)

Set scheme implementor - calls Protocol::implementor()

Example:


1 $ua->set_scheme_implementor(array('HTTPS'=>'Protocol_CURL'));

void set_scheme_implementor (array $array)
  • array $array:

    associative array with scheme as key and Protocol_* class as value.

set_timeout (line 302)

Set timeout

void set_timeout (int $timeout)
  • int $timeout
set_timeout_rw (line 323)

Set timeout read/write

void set_timeout_rw (int $timeout)
  • int $timeout
simple_request (line 877)

Simple request

This method dispatches a single WWW request on behalf of a user, and returns the response received. If differs from send_request() by automatically calling the prepare_request() method before the request is sent.

The arguments are the same as for send_request().

  • return:

    on error

object false &simple_request (object &$request, [mixed $arg = null])
  • object $request:

    passed by reference

  • mixed $arg:

    see send_request()

uncompress_content (line 1188)

Uncompress content

Note: only handles gzip encoding at the moment. This method will, if successful in uncompressing, replace the body of the response to hold the uncompressed content and remove the relevant encoding value from the 'content-encoding' header.

  • access: public
void uncompress_content (object &$response)
  • object $response:

    HTTP_Response

Documention generated on Wed, 16 Jul 2003 01:04:08 +0100 by phpDocumentor 1.2.0