Proposal for "Sync"

» Metadata » Status
» Description
HTTP_Sync package is wrapper around the HTTP_Request package.
All ideas of this package had been presented in the examples of HTTP_Request. I only accumulated them and insert some additions.

The main idea is to synchronize a local file with a remote source in according to lastModified time.

So:
If a local file exists and it's modification time greater than lastModified parameter the contents of file will be returned. If lastModified parameter is not set it equals to the result of time() function.
Otherwise HTTP_Sync will attempt to download data from a remote source, save it to a local file and returns a local file contents. If error occurs (e.g. a remote source is not reachable) the PEAR_Error instance will be returned.

To download large data the package includes the class inherited from HTTP_Request_Listener.

There are two examples of usage.

Example 1:
Synchronization of the local target file with the remote source as the static URL.
The target filename will get from the URL.

include_once 'HTTP/Sync.php';

$target = '';
$source = 'http://www.example.com/path/to/static/source.html';
$lastModified = strtotime('2006/06/06 06:06:06');

// HTTP request parameters
$params = array(
// HTTP_Request parameters
// (...)
);

// initialize object
$sync =& new HTTP_Sync($target, $source, $lastModified, $params);

// check local file (it exists and fresh)
// if it necessary to download and save
$result = $sync->synchronize();

// check result and handle it
if ( is_bool($result) ) {
// here any code to working with valid data
// (...)
$filename = $sync->getTarget();
echo file_get_contents($filename);
} else {
echo 'Error occurs:';
echo '<pre>' . print_r($result, true) . '</pre>';
}


Example 2:
Synchronization of the local target file with the remote source as the dynamic URL.
The local filename is set manually.

include_once 'HTTP/Sync.php';

$target = 'local-file-name';
$source = 'http://www.example.com/path/to/dynamic-source?with=any&params=here';
$lastModified = strtotime('2006/06/06 06:06:06');

// HTTP request parameters
$params = array(
// HTTP_Request parameters
// (...)
);

// initialize object
$sync =& new HTTP_Sync($target, $source, $lastModified, $params);

// check local file (it exists and fresh)
// if it necessary to download and save
$result = $sync->synchronize();

// check result and handle it
if ( is_bool($result) ) {
// here any code to working with valid data
// (...)
$filename = $sync->getTarget();
echo file_get_contents($filename);
} else {
echo 'Error occurs:';
echo '<pre>' . print_r($result, true) . '</pre>';
}
» Dependencies » Links
  • HTTP_Request
» Timeline » Changelog
  • First Draft: 2006-05-31
  • Proposal: 2006-05-31
  • Call for Votes: 2006-06-26
  • Ildar Shaimordanov
    [2006-06-07 04:21 UTC]

    The HTTP_Sync is wrapper for the HTTP_Request (previously it was extension of the HTTP_Request).

    The HTTP_Sync->getTarget() method had been added for defining of the actual target filename (useful for dynamic URL source or empty target argument)