cache() публичный Метод

Note that only the actual request is cached and not associated downloads, if any! Caching is disabled by default! the callback function to be executed for each and every request, as soon as a request finishes the callback function receives as argument an object with 4 properties (info, header, body and response) function mycallback($result) { everything went well at cURL level if ($result->response[1] == CURLE_OK) { if server responded with code 200 (meaning that everything went well) see http://httpstatus.es/ for a list of possible response codes if ($result->info['http_code'] == 200) { see all the returned data print_r('
');
            print_r($result);
show the server's response code
        } else die('Server responded with code ' . $result->info['http_code']);
something went wrong
($result still contains all data that could be gathered)
    } else die('cURL responded with: ' . $result->response[0]);

}
include the Zebra_cURL library
require 'path/to/Zebra_cURL';
instantiate the Zebra_cURL object
$curl = new Zebra_cURL();
cache results in the "cache" folder and for 86400 seconds (24 hours)
$curl->cache('cache', 86400);
let's fetch the RSS feeds of some popular tech-related websites
execute the "mycallback" function for each request, as soon as it finishes
$curl->get(array(
    'http://feeds.feedburner.com/alistapart/main',
    'http://feeds.feedburner.com/TechCrunch',
    'http://feeds.mashable.com/mashable',
), 'mycallback')
        
public cache ( string $path, integer $lifetime = 3600, boolean $compress = true, octal $chmod = 493 ) : void
$path string Path where cache files to be stored. Setting this to FALSE will disable caching. If set to a non-existing path, the library will try to create the folder and will trigger an error if, for whatever reasons, it is unable to do so. If the folder can be created, its permissions will be set to the value of $chmod @param integer $lifetime (Optional) The number of seconds after which cache will be considered expired. Default is 3600 (one hour). @param boolean $compress (Optional) If set to TRUE, cache files will be {@link http://php.net/manual/en/function.gzcompress.php gzcompress}-ed so that they occupy less disk space. Default is TRUE. @param octal $chmod (Optional) The file system permissions to be set for newly created cache files. I suggest using the value "0755" (without the quotes) but, if you know what you are doing, here is how you can calculate the permission levels: - 400 Owner Read - 200 Owner Write - 100 Owner Execute - 40 Group Read - 20 Group Write - 10 Group Execute - 4 Global Read - 2 Global Write - 1 Global Execute Default is "0755" (without the quotes). @return void
$lifetime integer
$compress boolean
$chmod octal
Результат void
Пример #1
0
function callback($result)
{
    // everything went well at cURL level
    if ($result->response[1] == CURLE_OK) {
        // if server responded with code 200 (meaning that everything went well)
        // see http://httpstatus.es/ for a list of possible response codes
        if ($result->info['http_code'] == 200) {
            // see all the returned data
            // remember, that the "body" property of $result, unless specifically disabled in the library's constructor,
            // is run through "htmlentities()", so you may want to "html_entity_decode" it
            print_r('<pre>');
            print_r($result->info);
            // show the server's response code
        } else {
            die('Server responded with code ' . $result->info['http_code']);
        }
        // something went wrong
        // ($result still contains all data that could be gathered)
    } else {
        die('cURL responded with: ' . $result->response[0]);
    }
}
// include the library
require '../Zebra_cURL.php';
// instantiate the Zebra_cURL class
$curl = new Zebra_cURL();
// cache results 3600 seconds
$curl->cache('cache', 3600);
// get RSS feeds of some popular tech websites
$curl->get(array('http://rss1.smashingmagazine.com/feed/', 'http://allthingsd.com/feed/', 'http://feeds.feedburner.com/nettuts', 'http://feeds.feedburner.com/alistapart/main'), 'callback');
Пример #2
0
<?php

function callback($result)
{
    // results from twitter is json-encoded;
    // remember, the "body" property of $result is run through "htmlentities()" so we need to "html_entity_decode" it
    $result->body = json_decode(html_entity_decode($result->body));
    // show everything
    print_r('<pre>');
    print_r($result->info);
}
// include the library
require '../Zebra_cURL.php';
// instantiate the Zebra_cURL class
$curl = new Zebra_cURL();
// cache results 60 seconds
$curl->cache('cache', 60);
// search twitter for the "jquery" hashtag
$curl->get('http://search.twitter.com/search.json?q=' . urlencode('#jquery'), 'callback');