[ Index ]
 

Code source de PHP PEAR 1.4.5

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/Cache/ -> HTTP_Request.php (sommaire)

(pas de description)

Poids: 296 lignes (10 kb)
Inclus ou requis:0 fois
Référencé: 0 fois
Nécessite: 2 fichiers
 Cache.php
 HTTP/Request.php

Définit 1 class

Cache_HTTP_Request:: (8 méthodes):
  Cache_HTTP_Request()
  _Cache_HTTP_Request()
  getResponseBody()
  getResponseCode()
  getResponseHeader()
  setMode()
  sendRequest()
  _sendRequestAndGetResponse()


Classe: Cache_HTTP_Request  - X-Ref

HTTP_Request Cache

The classical example is :

You want to get news from another site through RSS remote files. But you
don't want to access to to the remote site at every time you display
its news on your site. Because, if the remote site is down or slow...
So you you need a class which makes a local cache copy of the remote file.
Every x hours, the cache is updated. But if the remote site is down, the
local cache copy is keeped (you can also get error messages if you want).

So you need this class!

Cache_HTTP_Request inherits from Cache and use HTTP_Request to access to
the remote file.

Usage example :

<?php
require_once('Cache/HTTP_Request.php');

$cache = &new Cache_HTTP_Request('http://www.php.net', NULL, 'file', NULL, 3600);
$cache->sendRequest();
$remoteFileBody = $cache->getResponseBody();

(...)
?>

Cache_HTTP_Request($url, $params = NULL, $container = 'file',$containerOptions = NULL, $expires = 3600,$mode = CACHE_HTTP_REQUEST_KEEP_LOCAL_COPY)   X-Ref
Constructor

param: $url The url to access
param: $params Associative array of parameters which can be:
param: string $container Name of container class
param: array $containerOptions Array with container class options
param: int $mode What to do when the remote server is down :
param: int $expires lifetime of the cached data in seconds - 0 for endless

_Cache_HTTP_Request()   X-Ref
Deconstructor


getResponseBody()   X-Ref
Get and return the response body (NULL if no data available)

return: mixed response body

getResponseCode()   X-Ref
Get and return the response code (NULL if no data available)

return: mixed response code

getResponseHeader()   X-Ref
Get and return the response header (NULL if no data available)

return: mixed response header

setMode($newMode)   X-Ref
Set a new mode when the server is down

param: int $newMode What to do when the remote server is down :

sendRequest()   X-Ref
Send the HTTP request or use the cache system

If there is a cache file for this HTTP request, the request is not re-sent.
Cached response is used. Yet, if the cache is expired, the HTTP request
is re-sent. Then, if the remote server is down, this method will return :
(depending on the selected mode)
- false or
- a PEAR_Error or (better)
- true and the local copy of the latest valid response will be used.

(technical)
For the last choice, there is a technical tips.
Indeed, there are two cache files. The first one (id key) is a classical one
with the given lifetime. But it can be removed by automatic garbage collection
for example. So to be able to use the latest valid response (when the remote
server is dead), we make a second cache file with no lifetime (id2 key).

return: mixed true or false or a PEAR_ERROR

_sendRequestAndGetResponse()   X-Ref
Send HTTP request and get the response

return: boolean success or not ?



Généré le : Sun Feb 25 14:08:00 2007 par Balluche grâce à PHPXref 0.7