PHP Class Zebra_cURL

Zebra_cURL is a high performance PHP library acting as a wrapper to PHP's {@link http://www.php.net/manual/en/book.curl.php libcurl library}, which not only allows the running of multiple requests at once asynchronously, in parallel, but also as soon as one thread finishes it can be processed right away without having to wait for the other threads in the queue to finish. Also, each time a request is completed another one is added to the queue, thus keeping a constant number of threads running at all times and eliminating wasted CPU cycles from busy waiting. This result is a faster and more efficient way of processing large quantities of cURL requests (like fetching thousands of RSS feeds at once), drastically reducing processing time. This script supports GET and POST request, basic downloads, downloads from FTP servers, HTTP Authentication, and requests through proxy servers. For maximum efficiency downloads are streamed (bytes downloaded are directly written to disk) removing the unnecessary strain from the server of having to read files into memory first, and then writing them to disk. Zebra_cURL requires the {@link http://www.php.net/manual/en/curl.installation.php PHP cURL extension} to be enabled. The code is heavily commented and generates no warnings/errors/notices when PHP's error reporting level is set to {@link http://www.php.net/manual/en/function.error-reporting.php E_ALL}. Visit {@link http://stefangabos.ro/php-libraries/zebra-curl/} for more information. For more resources visit {@link http://stefangabos.ro/}
Mostra file Open project: stefangabos/zebra_curl Class Usage Examples

Public Properties

Property Type Description
$pause_interval integer If the value of this property is greater than 0, the library will process as many requests as defined by the {@link threads} property, and then wait for {@link pause_interval pause_interval} seconds before processing the next batch of requests. Default is 0 (the library will keep as many parallel threads as defined by {@link threads} running at all times, until there are no more requests to process).
$threads integer process 30 simultaneous requests, at all times $curl->threads = 30; Note that, unless {@link pause_interval} is set to a value greater than 0, the library will process a constant number of requests, at all times; it's doing this by processing a new request as soon as another one finishes, instead of waiting for each batch to finish, and so on, until there are no more requests to process, and thus greatly decreasing execution time. If {@link pause_interval} is set to a value greater than 0, the library will process as many requests as set by the {@link threads} property and then will wait for {@link pause_interval} seconds before processing the next batch of requests. Default is 10.

Public Methods

Method Description
__construct ( boolean $htmlentities = true ) : void Constructor of the class.
cache ( string $path, integer $lifetime = 3600, boolean $compress = true, octal $chmod = 493 ) : void Use this method to enable caching of requests.
cookies ( string $path ) : void Sets the path and name of the file to save to / retrieve cookies from. All cookie data will be stored in this file on a per-domain basis. Important when cookies need to stored/restored to maintain status/session of requests made to the same domains.
delete ( mixed $urls, mixed $callback = '' ) : void Performs an HTTP DELETE request to one or more URLs with the POST data as specified by the $urls argument, and executes the callback function specified by the $callback argument for each and every request, as soon as a request finishes.
download ( mixed $urls, string $path, mixed $callback = '' ) : void Downloads one or more files from one or more URLs specified by the $urls argument, saves the downloaded files (with their original name) to the path specified by the $path argument, and executes the callback function specified by the $callback argument for each and every request, as soon as a request finishes.
ftp_download ( mixed $urls, string $path, string $username = '', string $password = '', mixed $callback = '' ) : void Works exactly like the {@link download} method only that downloads are made from an FTP server.
get ( mixed $urls, mixed $callback = '' ) : void Performs an HTTP GET request to one or more URLs specified by the $urls argument and executes the callback function specified by the $callback argument for each and every request, as soon as a request finishes.
header ( mixed $urls, mixed $callback = '' ) : void Works exactly like the {@link get} method, the only difference being that this method will only return the headers, without body.
http_authentication ( string $username = '', string $password = '', string $type = CURLAUTH_ANY ) : void Use this method to make requests to pages that require prior HTTP authentication.
option ( mixed $option, mixed $value = '' ) : void Allows you to set one or more {@link http://php.net/manual/en/function.curl-setopt.php cURL options}.
post ( mixed $urls, mixed $callback = '' ) : void Performs an HTTP POST request to one or more URLs with the POST data as specified by the $urls argument, and executes the callback function specified by the $callback argument for each and every request, as soon as a request finishes.
proxy ( string $proxy, string $port = 80, string $username = '', string $password = '' ) : void Instruct the library to tunnel all requests through a proxy server.
put ( mixed $urls, mixed $callback = '' ) : void Performs an HTTP PUT request to one or more URLs with the POST data as specified by the $urls argument, and executes the callback function specified by the $callback argument for each and every request, as soon as a request finishes.
queue ( ) : void Instructs the library to queue requests rather than processing them right away. Useful for grouping different types of requests and treat them as a single request.
scrap ( string $url, boolean $body_only = false ) : mixed A shorthand for making a single {@link get} request without the need of a callback function
ssl ( boolean $verify_peer = true, integer $verify_host = 2, mixed $file = false, mixed $path = false ) : void Requests made to HTTPS servers sometimes require additional configuration, depending on the server. Most of the times {@link __construct() the defaults} set by the library will get you through, but if defaults are not working, you can set specific options using this method.
start ( ) : void Executes queued requests.

Private Methods

Method Description
_debug ( ) : string Returns the currently set options in "human-readable" format.
_get_cache_file_name ( $request ) : string Returns the cache file name associated with a specific request.
_parse_headers ( string $headers ) : mixed Parse response headers.
_process ( ) : void Does the actual work.
_process_paused ( ) : void A wrapper for the {@link _process} method used when we need to pause between batches of requests to process.
_queue_requests ( ) : void A helper method used by the {@link _process} method, taking care of keeping a constant number of requests queued, so that as soon as one request finishes another one will instantly take its place, thus making sure that the maximum allowed number of parallel threads are running all the time.
_user_agent ( ) : void Generates a (slightly) random user agent (Internet Explorer 9 or 10, on Windows Vista, 7 or 8, with other extra strings)

Method Details

__construct() public method

Below is the list of default options set by the library when instantiated: - CURLINFO_HEADER_OUT - TRUE; get the last request header; if set to FALSE the "last_request" entry of the "headers" attribute of the object given as argument to the callback function, will be an empty string; - CURLOPT_AUTOREFERER - TRUE; automatically set the Referer: field in requests where it follows a Location: redirect; - CURLOPT_COOKIEFILE - empty string; no cookies are loaded, but cookie handling is still enabled - CURLOPT_CONNECTTIMEOUT - 10; the number of seconds to wait while trying to connect. use 0 to wait indefinitely; - CURLOPT_ENCODING - gzip,deflate; the contents of the "Accept-Encoding:" header; it enables decoding of the response - CURLOPT_FOLLOWLOCATION - TRUE; automatically follow any Location: header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many Location: headers as specified by the value of CURLOPT_MAXREDIRS - see below); - CURLOPT_HEADER - TRUE; get the response header(s); if set to FALSE the "responses" entry of the "headers" attribute of the object given as argument to the callback function, will be an empty string; - CURLOPT_MAXREDIRS - 50; the maximum amount of HTTP redirections to follow; used together with CURLOPT_FOLLOWLOCATION; - CURLOPT_RETURNTRANSFER - TRUE; return the transfer's body as a string instead of outputting it directly; if set to FALSE the "body" attribute of the object given as argument to a callback function will be an empty string; - CURLOPT_SSL_VERIFYHOST - 2; check the existence of a common name in the SSL peer certificate (for when connecting to HTTPS), and that it matches with the provided hostname; see also the {@link ssl} method; - CURLOPT_SSL_VERIFYPEER - FALSE; stop cURL from verifying the peer's certificate (which would most likely cause the request to fail). see also the {@link ssl} method; - CURLOPT_TIMEOUT - 10; the maximum number of seconds to allow cURL functions to execute; - CURLOPT_USERAGENT - A (slightly) random user agent (Internet Explorer 9 or 10, on Windows Vista, 7 or 8, with other extra strings). Some web services will not respond unless a valid user-agent string is provided
public __construct ( boolean $htmlentities = true ) : void
$htmlentities boolean Instructs the script whether the response body returned by the {@link get} and {@link post} methods should be run through PHP's {@link http://php.net/manual/en/function.htmlentities.php htmlentities} function. @return void
return void

cache() public method

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
return void

cookies() public method

This method will automatically set the CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE options.
public cookies ( string $path ) : void
$path string The path to a file to save to / retrieve cookies from. If file does not exist the library will attempt to create it, and if it is unable to create it will trigger an error. @return void
return void

delete() public method

This method will automatically set the following options: - CURLINFO_HEADER_OUT - TRUE - CURLOPT_CUSTOMREQUEST - "DELETE" - CURLOPT_HEADER - TRUE - CURLOPT_NOBODY - FALSE - CURLOPT_POST - FALSE - CURLOPT_POSTFIELDS - the POST data ...and will unset the following options: - CURLOPT_BINARYTRANSFER - CURLOPT_HTTPGET - TRUE - CURLOPT_FILE Multiple requests are processed asynchronously, in parallel, and the callback function is called for each and every request, as soon as a request finishes. The number of parallel requests to be constantly processed, at all times, can be set through the {@link threads} property. See also the {@link pause_interval} property. Note that requests may not finish in the same order as initiated! 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();
do a PUT and execute the "mycallback" function for each
request, as soon as it finishes
$curl->delete(array(
    'http://www.somewebsite.com'  =>  array(
        'data_1'  =>  'value 1',
        'data_2'  =>  'value 2',
    ),
), 'mycallback');
        
public delete ( mixed $urls, mixed $callback = '' ) : void
$urls mixed An associative array in the form of url => delete-data, where "delete-data" is an associative array in the form of name => value. "delete-data" can also be an arbitrary string - useful if you want to send raw data (like a JSON) The Content-Type header will be set to multipart/form-data. @param mixed $callback (Optional) Callback function to be called as soon as a request finishes. May be given as a string representing a name of an existing function, as an anonymous function created on the fly via {@link http://www.php.net/manual/en/function.create-function.php create_function} or via a {@link http://www.php.net/manual/en/function.create-function.php closure}. The callback function receives as first argument an object with 4 properties as described below, while any further arguments passed to the {@link delete} method will be passed as extra arguments to the callback function: - info - an associative array containing information about the request that just finished, as returned by PHP's {@link http://php.net/manual/en/function.curl-getinfo.php curl_getinfo()} function; - headers - an associative array with 2 items: - last_request an array with a single entry containing the request headers generated by the last request; so, remember, if there are redirects involved, there will be more requests made, but only information from the last one will be available; if explicitly disabled via the {@link option} method by setting CURLINFO_HEADER_OUT to 0 or FALSE, this will be an empty string; - responses an array with one or more entries (if there are redirects involved) with the response headers of all the requests made; if explicitly disabled via the {@link option} method by setting CURLOPT_HEADER to 0 or FALSE, this will be an empty string; Unless disabled, each entry in the headers' array is an associative array in the form of property => value - body - the response of the request (the content of the page at the URL). Unless disabled via the {@link __construct() constructor}, all applicable characters will be converted to HTML entities via PHP's {@link http://php.net/manual/en/function.htmlentities.php htmlentities} function, so remember to use PHP's {@link http://www.php.net/manual/en/function.html-entity-decode.php html_entity_decode} function to do reverse this, if it's the case; If "body" is explicitly disabled via the {@link option} method by setting CURLOPT_NOBODY to 0 or FALSE, this will be an empty string; - response - the response given by the cURL library as an array with 2 entries: the first entry is the textual representation of the result's code, while second is the result's code itself; if the request was successful, these values will be array(CURLE_OK, 0); consult {@link http://www.php.net/manual/en/function.curl-errno.php#103128 this list} to see the possible values of this property; If the callback function returns FALSE while {@link cache} is enabled, the library will not cache the respective request, making it easy to retry failed requests without having to clear all cache. @since 1.3.3 @return void
$callback mixed
return void

download() public method

Downloads are streamed (bytes downloaded are directly written to disk) removing the unnecessary strain from your server of reading files into memory first, and then writing them to disk. This method will automatically set the following options: - CURLINFO_HEADER_OUT - TRUE - CURLOPT_BINARYTRANSFER - TRUE - CURLOPT_HEADER - TRUE - CURLOPT_FILE ...and will unset the following options: - CURLOPT_CUSTOMREQUEST - CURLOPT_HTTPGET - CURLOPT_NOBODY - CURLOPT_POST - CURLOPT_POSTFIELDS Files are downloaded preserving their original names, so you may want to check that if you are downloading more files having the same name! Multiple requests are processed asynchronously, in parallel, and the callback function is called for each and every request, as soon as a request finishes. The number of parallel requests to be constantly processed, at all times, can be set through the {@link threads} property. See also the {@link pause_interval} property. Note that requests may not finish in the same order as initiated! 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();
download 2 images from 2 different websites, and
execute the "mycallback" function for each request, as soon as it finishes
$curl->download(array(
    'http://www.somewebsite.com/images/alpha.jpg',
    'http://www.otherwebsite.com/images/omega.jpg',
), 'destination/path/', 'mycallback');
        
public download ( mixed $urls, string $path, mixed $callback = '' ) : void
$urls mixed A single URL or an array of URLs to process. @param string $path The path to where to save the file(s) to. If path is not pointing to a directory or is not writable, the library will trigger an error. @param mixed $callback (Optional) Callback function to be called as soon as a request finishes. May be given as a string representing a name of an existing function, as an anonymous function created on the fly via {@link http://www.php.net/manual/en/function.create-function.php create_function} or via a {@link http://www.php.net/manual/en/function.create-function.php closure}. The callback function receives as first argument an object with 4 properties as described below, while any further arguments passed to the {@link download} method will be passed as extra arguments to the callback function: - info - an associative array containing information about the request that just finished, as returned by PHP's {@link http://php.net/manual/en/function.curl-getinfo.php curl_getinfo} function; there's also an extra entry called original_url because, as curl_getinfo() only returns information about the last request, the original URL may be lost otherwise. - headers - an associative array with 2 items: - last_request an array with a single entry containing the request headers generated by the last request; so, remember, if there are redirects involved, there will be more requests made, but only information from the last one will be available; if explicitly disabled via the {@link option} method by setting CURLINFO_HEADER_OUT to 0 or FALSE, this will be an empty string; - responses an empty string as it is not available for this method; Unless disabled, each entry in the headers' array is an associative array in the form of property => value - body - an empty string as it is not available for this method; - response - the response given by the cURL library as an array with 2 entries: the first entry is the textual representation of the result's code, while second is the result's code itself; if the request was successful, these values will be array(CURLE_OK, 0); consult {@link http://www.php.net/manual/en/function.curl-errno.php#103128 this list} to see the possible values of this property; If the callback function returns FALSE while {@link cache} is enabled, the library will not cache the respective request, making it easy to retry failed requests without having to clear all cache. @return void
$path string
$callback mixed
return void

ftp_download() public method

Downloads from an FTP server to which the connection is made using the given $username and $password arguments, one or more files specified by the $urls argument, saves the downloaded files (with their original name) to the path specified by the $path argument, and executes the callback function specified by the $callback argument for each and every request, as soon as a request finishes. Downloads are streamed (bytes downloaded are directly written to disk) removing the unnecessary strain from your server of reading files into memory first, and then writing them to disk. This method will automatically set the following options: - CURLINFO_HEADER_OUT - TRUE - CURLOPT_BINARYTRANSFER - TRUE - CURLOPT_HEADER - TRUE - CURLOPT_FILE ...and will unset the following options: - CURLOPT_CUSTOMREQUEST - CURLOPT_HTTPGET - CURLOPT_NOBODY - CURLOPT_POST - CURLOPT_POSTFIELDS Files are downloaded preserving their name so you may want to check that, if you are downloading more files having the same name (either from the same, or from different servers)! Multiple requests are processed asynchronously, in parallel, and the callback function is called for each and every request, as soon as a request finishes. The number of parallel requests to be constantly processed, at all times, can be set through the {@link threads} property. See also the {@link pause_interval} property. Note that requests may not finish in the same order as initiated! 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();
connect to the FTP server using the given credential, download a file to a given location and
execute the "mycallback" function for each request, as soon as it finishes
$curl->ftp_download('ftp://somefile.ext', 'destination/path', 'username', 'password', 'mycallback');
        
public ftp_download ( mixed $urls, string $path, string $username = '', string $password = '', mixed $callback = '' ) : void
$urls mixed A single URL or an array of URLs to process. @param string $path The path to where to save the file(s) to. If path is not pointing to a directory or is not writable, the library will trigger an error. @param string $username (Optional) The username to be used to connect to the FTP server (if required). @param string $password (Optional) The password to be used to connect to the FTP server (if required). @param mixed $callback (Optional) Callback function to be called as soon as a request finishes. May be given as a string representing a name of an existing function, as an anonymous function created on the fly via {@link http://www.php.net/manual/en/function.create-function.php create_function} or via a {@link http://www.php.net/manual/en/function.create-function.php closure}. The callback function receives as first argument an object with 4 properties as described below, while any further arguments passed to the {@link ftp_download} method will be passed as extra arguments to the callback function: - info - an associative array containing information about the request that just finished, as returned by PHP's {@link http://php.net/manual/en/function.curl-getinfo.php curl_getinfo} function; - headers - an associative array with 2 items: - last_request an array with a single entry containing the request headers generated by the last request; so, remember, if there are redirects involved, there will be more requests made, but only information from the last one will be available; if explicitly disabled via the {@link option} method by setting CURLINFO_HEADER_OUT to 0 or FALSE, this will be an empty string; - responses an empty string as it is not available for this method; Unless disabled, each entry in the headers' array is an associative array in the form of property => value - body - an empty string as it is not available for this method; - response - the response given by the cURL library as an array with 2 entries: the first entry is the textual representation of the result's code, while second is the result's code itself; if the request was successful, these values will be array(CURLE_OK, 0); consult {@link http://www.php.net/manual/en/function.curl-errno.php#103128 this list} to see the possible values of this property; If the callback function returns FALSE while {@link cache} is enabled, the library will not cache the respective request, making it easy to retry failed requests without having to clear all cache. @return void
$path string
$username string
$password string
$callback mixed
return void

get() public method

This method will automatically set the following options: - CURLINFO_HEADER_OUT - TRUE - CURLOPT_HEADER - TRUE - CURLOPT_HTTPGET - TRUE - CURLOPT_NOBODY - FALSE ...and will unset the following options: - CURLOPT_BINARYTRANSFER - CURLOPT_CUSTOMREQUEST - CURLOPT_FILE - CURLOPT_POST - CURLOPT_POSTFIELDS Multiple requests are processed asynchronously, in parallel, and the callback function is called for each and every request, as soon as a request finishes. The number of parallel requests to be constantly processed, at all times, can be set through the {@link threads} property. See also the {@link pause_interval} property. Note that requests may not finish in the same order as initiated! 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 3600 seconds (one hour)
$curl->cache('cache', 3600);
let's fetch the RSS feeds of some popular 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 get ( mixed $urls, mixed $callback = '' ) : void
$urls mixed A single URL or an array of URLs to process. @param mixed $callback (Optional) Callback function to be called as soon as a request finishes. May be given as a string representing a name of an existing function, as an anonymous function created on the fly via {@link http://www.php.net/manual/en/function.create-function.php create_function} or via a {@link http://www.php.net/manual/en/function.create-function.php closure}. The callback function receives as first argument an object with 4 properties as described below, while any further arguments passed to the {@link get} method will be passed as extra arguments to the callback function: - info - an associative array containing information about the request that just finished, as returned by PHP's {@link http://php.net/manual/en/function.curl-getinfo.php curl_getinfo} function; - headers - an associative array with 2 items: - last_request an array with a single entry containing the request headers generated by the last request; so, remember, if there are redirects involved, there will be more requests made, but only information from the last one will be available; - responses an array with one or more entries (if there are redirects involved) with the response headers of all the requests made; Each entry in the headers' array is an associative array in the form of property => value - body - the response of the request (the content of the page at the URL). Unless disabled via the {@link __construct() constructor}, all applicable characters will be converted to HTML entities via PHP's {@link http://php.net/manual/en/function.htmlentities.php htmlentities} function, so remember to use PHP's {@link http://www.php.net/manual/en/function.html-entity-decode.php html_entity_decode} function to do reverse this, if it's the case; - response - the response given by the cURL library as an array with 2 entries: the first entry is the textual representation of the result's code, while second is the result's code itself; if the request was successful, these values will be array(CURLE_OK, 0); consult {@link http://www.php.net/manual/en/function.curl-errno.php#103128 this list} to see the possible values of this property; If the callback function returns FALSE while {@link cache} is enabled, the library will not cache the respective request, making it easy to retry failed requests without having to clear all cache. @return void
$callback mixed
return void

header() public method

This method will automatically set the following options: - CURLINFO_HEADER_OUT - TRUE - CURLOPT_HEADER - TRUE - CURLOPT_HTTPGET - TRUE - CURLOPT_NOBODY - TRUE ...and will unset the following options: - CURLOPT_BINARYTRANSFER - CURLOPT_CUSTOMREQUEST - CURLOPT_FILE - CURLOPT_POST - CURLOPT_POSTFIELDS Multiple requests are processed asynchronously, in parallel, and the callback function is called for each and every request, as soon as a request finishes. The number of parallel requests to be constantly processed, at all times, can be set through the {@link threads} property. See also the {@link pause_interval} property. Note that requests may not finish in the same order as initiated! 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();
process given URLs execute the "mycallback" function for each
request, as soon as it finishes
$curl->header('http://www.somewebsite.com', 'mycallback');
        
public header ( mixed $urls, mixed $callback = '' ) : void
$urls mixed A single URL or an array of URLs to process. @param mixed $callback (Optional) Callback function to be called as soon as a request finishes. May be given as a string representing a name of an existing function, as an anonymous function created on the fly via {@link http://www.php.net/manual/en/function.create-function.php create_function} or via a {@link http://www.php.net/manual/en/function.create-function.php closure}. The callback function receives as first argument an object with 4 properties as described below, while any further arguments passed to the {@link header} method will be passed as extra arguments to the callback function: - info - an associative array containing information about the request that just finished, as returned by PHP's {@link http://php.net/manual/en/function.curl-getinfo.php curl_getinfo} function; - headers - an associative array with 2 items: - last_request an array with a single entry containing the request headers generated by the last request; so, remember, if there are redirects involved, there will be more requests made, but only information from the last one will be available; - responses an array with one or more entries (if there are redirects involved) with the response headers of all the requests made; Each entry in the headers' array is an associative array in the form of property => value - body - an empty string as it is not available for this method; - response - the response given by the cURL library as an array with 2 entries: the first entry is the textual representation of the result's code, while second is the result's code itself; if the request was successful, these values will be array(CURLE_OK, 0); consult {@link http://www.php.net/manual/en/function.curl-errno.php#103128 this list} to see the possible values of this property; If the callback function returns FALSE while {@link cache} is enabled, the library will not cache the respective request, making it easy to retry failed requests without having to clear all cache. @return void
$callback mixed
return void

http_authentication() public method

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();
prepare user name and password
$curl->http_authentication('username', 'password');
get content from a page that requires prior HTTP authentication
$curl->get('http://www.some-page-requiring-prior-http-authentication.com', 'mycallback');


If you have to unset previously set values use

$curl->http_authentication();
        
public http_authentication ( string $username = '', string $password = '', string $type = CURLAUTH_ANY ) : void
$username string User name to be used for authentication. @param string $password Password to be used for authentication. @param string $type (Optional) The HTTP authentication method(s) to use. The options are: - CURLAUTH_BASIC - CURLAUTH_DIGEST - CURLAUTH_GSSNEGOTIATE - CURLAUTH_NTLM - CURLAUTH_ANY - CURLAUTH_ANYSAFE The bitwise | (or) operator can be used to combine more than one method. If this is done, cURL will poll the server to see what methods it supports and pick the best one. CURLAUTH_ANY is an alias for CURLAUTH_BASIC | CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM. CURLAUTH_ANYSAFE is an alias for CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM. Default is CURLAUTH_ANY. @return void
$password string
$type string
return void

option() public method

include the Zebra_cURL library require 'path/to/Zebra_cURL'; instantiate the Zebra_cURL object $curl = new Zebra_cURL(); setting a single option $curl->option(CURLOPT_CONNECTTIMEOUT, 10); setting multiple options at once $curl->option(array( CURLOPT_TIMEOUT => 10, CURLOPT_CONNECTTIMEOUT => 10, )); make a request here...
public option ( mixed $option, mixed $value = '' ) : void
$option mixed A single option for which to set a value, or an associative array in the form of option => value (in case of an array, the $value argument will be disregarded). Setting a value to null will "unset" that option. @param mixed $value (Optional) If the $option argument is not an array, then this argument represents the value to be set for the respective option. If the $option argument is an array, then the value of this argument will be ignored. Setting a value to null will "unset" that option. @return void
$value mixed
return void

post() public method

This method will automatically set the following options: - CURLINFO_HEADER_OUT - TRUE - CURLOPT_HEADER - TRUE - CURLOPT_NOBODY - FALSE - CURLOPT_POST - TRUE - CURLOPT_POSTFIELDS - the POST data ...and will unset the following options: - CURLOPT_BINARYTRANSFER - CURLOPT_CUSTOMREQUEST - CURLOPT_HTTPGET - TRUE - CURLOPT_FILE Multiple requests are processed asynchronously, in parallel, and the callback function is called for each and every request, as soon as a request finishes. The number of parallel requests to be constantly processed, at all times, can be set through the {@link threads} property. See also the {@link pause_interval} property. Note that requests may not finish in the same order as initiated! 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();
do a POST and execute the "mycallback" function for each
request, as soon as it finishes
$curl->post(array(
    'http://www.somewebsite.com'  =>  array(
        'data_1'  =>  'value 1',
        'data_2'  =>  'value 2',
    ),
), 'mycallback');
note that we're also uploading a file this time
and note that we're prefixing the file name with @
$curl->post(array(
    'http://www.somewebsite.com'  =>  array(
        'data_1'  =>  'value 1',
        'data_2'  =>  'value 2',
        'data_3'  =>  '@absolute/path/to/file.ext',
), 'mycallback');
        
public post ( mixed $urls, mixed $callback = '' ) : void
$urls mixed An associative array in the form of url => post-data, where "post-data" is an associative array in the form of name => value. "post-data" can also be an arbitrary string - useful if you want to send raw data (like a JSON) To post a file, prepend the filename with @ and use the full server path. For PHP 5.5+ files are uploaded using {@link http://php.net/manual/ro/class.curlfile.php CURLFile} and {@link https://wiki.php.net/rfc/curl-file-upload CURLOPT_SAFE_UPLOAD} will be set to TRUE. For lower PHP versions, files will be uploaded the "old" way and the file's mime type should be explicitly specified by following the filename with the type in the format ';type=mimetype'. as most of the times cURL will send the wrong mime type... The Content-Type header will be set to multipart/form-data. @param mixed $callback (Optional) Callback function to be called as soon as a request finishes. May be given as a string representing a name of an existing function, as an anonymous function created on the fly via {@link http://www.php.net/manual/en/function.create-function.php create_function} or via a {@link http://www.php.net/manual/en/function.create-function.php closure}. The callback function receives as first argument an object with 4 properties as described below, while any further arguments passed to the {@link post} method will be passed as extra arguments to the callback function: - info - an associative array containing information about the request that just finished, as returned by PHP's {@link http://php.net/manual/en/function.curl-getinfo.php curl_getinfo} function; - headers - an associative array with 2 items: - last_request an array with a single entry containing the request headers generated by the last request; so, remember, if there are redirects involved, there will be more requests made, but only information from the last one will be available; if explicitly disabled via the {@link option} method by setting CURLINFO_HEADER_OUT to 0 or FALSE, this will be an empty string; - responses an array with one or more entries (if there are redirects involved) with the response headers of all the requests made; if explicitly disabled via the {@link option} method by setting CURLOPT_HEADER to 0 or FALSE, this will be an empty string; Unless disabled, each entry in the headers' array is an associative array in the form of property => value - body - the response of the request (the content of the page at the URL). Unless disabled via the {@link __construct() constructor}, all applicable characters will be converted to HTML entities via PHP's {@link http://php.net/manual/en/function.htmlentities.php htmlentities} function, so remember to use PHP's {@link http://www.php.net/manual/en/function.html-entity-decode.php html_entity_decode} function to do reverse this, if it's the case; If "body" is explicitly disabled via the {@link option} method by setting CURLOPT_NOBODY to 0 or FALSE, this will be an empty string; - response - the response given by the cURL library as an array with 2 entries: the first entry is the textual representation of the result's code, while second is the result's code itself; if the request was successful, these values will be array(CURLE_OK, 0); consult {@link http://www.php.net/manual/en/function.curl-errno.php#103128 this list} to see the possible values of this property; If the callback function returns FALSE while {@link cache} is enabled, the library will not cache the respective request, making it easy to retry failed requests without having to clear all cache. @return void
$callback mixed
return void

proxy() public method

the callback function to be executed for each and every request, as soon as a request finishes 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 class
$curl = new Zebra_cURL();
connect to a proxy server
(that's a random one i got from http://www.hidemyass.com/proxy-list/)
$curl->proxy('187.63.32.250', '3128');
fetch a page
$curl->get('http://www.somewebsite.com/', 'mycallback');
        
public proxy ( string $proxy, string $port = 80, string $username = '', string $password = '' ) : void
$proxy string The HTTP proxy to tunnel requests through. Can be an URL or an IP address. This option can also be set using the {@link option} method and setting CURLOPT_PROXY option to the desired value. Setting this argument to FALSE will "unset" all the proxy-related options. @param string $port (Optional) The port number of the proxy to connect to. Default is 80. This option can also be set using the {@link option} method and setting CURLOPT_PROXYPORT option to the desired value. @param string $username (Optional) The username to be used for the connection to the proxy (if required by the proxy) Default is "" (an empty string) The username and the password can also be set using the {@link option} method and setting CURLOPT_PROXYUSERPWD option to the desired value formatted like [username]:[password]. . @param string $password (Optional) The password to be used for the connection to the proxy (if required by the proxy) Default is "" (an empty string) The username and the password can also be set using the {@link option} method and setting CURLOPT_PROXYUSERPWD option to the desired value formatted like [username]:[password]. . @return void
$port string
$username string
$password string
return void

put() public method

This method will automatically set the following options: - CURLINFO_HEADER_OUT - TRUE - CURLOPT_CUSTOMREQUEST - "PUT" - CURLOPT_HEADER - TRUE - CURLOPT_NOBODY - FALSE - CURLOPT_POST - FALSE - CURLOPT_POSTFIELDS - the POST data ...and will unset the following options: - CURLOPT_BINARYTRANSFER - CURLOPT_HTTPGET - TRUE - CURLOPT_FILE Multiple requests are processed asynchronously, in parallel, and the callback function is called for each and every request, as soon as a request finishes. The number of parallel requests to be constantly processed, at all times, can be set through the {@link threads} property. See also the {@link pause_interval} property. Note that requests may not finish in the same order as initiated! 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();
do a PUT and execute the "mycallback" function for each
request, as soon as it finishes
$curl->put(array(
    'http://www.somewebsite.com'  =>  array(
        'data_1'  =>  'value 1',
        'data_2'  =>  'value 2',
    ),
), 'mycallback');
        
public put ( mixed $urls, mixed $callback = '' ) : void
$urls mixed An associative array in the form of url => put-data, where "put-data" is an associative array in the form of name => value. "put-data" can also be an arbitrary string - useful if you want to send raw data (like a JSON) To put a file, prepend the filename with @ and use the full server path. For PHP 5.5+ files are uploaded using {@link http://php.net/manual/ro/class.curlfile.php CURLFile} and {@link https://wiki.php.net/rfc/curl-file-upload CURLOPT_SAFE_UPLOAD} will be set to TRUE. For lower PHP versions, files will be uploaded the "old" way and the file's mime type should be explicitly specified by following the filename with the type in the format ';type=mimetype'. as most of the times cURL will send the wrong mime type... The Content-Type header will be set to multipart/form-data. @param mixed $callback (Optional) Callback function to be called as soon as a request finishes. May be given as a string representing a name of an existing function, as an anonymous function created on the fly via {@link http://www.php.net/manual/en/function.create-function.php create_function} or via a {@link http://www.php.net/manual/en/function.create-function.php closure}. The callback function receives as first argument an object with 4 properties as described below, while any further arguments passed to the {@link put} method will be passed as extra arguments to the callback function: - info - an associative array containing information about the request that just finished, as returned by PHP's {@link http://php.net/manual/en/function.curl-getinfo.php curl_getinfo} function; - headers - an associative array with 2 items: - last_request an array with a single entry containing the request headers generated by the last request; so, remember, if there are redirects involved, there will be more requests made, but only information from the last one will be available; if explicitly disabled via the {@link option} method by setting CURLINFO_HEADER_OUT to 0 or FALSE, this will be an empty string; - responses an array with one or more entries (if there are redirects involved) with the response headers of all the requests made; if explicitly disabled via the {@link option} method by setting CURLOPT_HEADER to 0 or FALSE, this will be an empty string; Unless disabled, each entry in the headers' array is an associative array in the form of property => value - body - the response of the request (the content of the page at the URL). Unless disabled via the {@link __construct() constructor}, all applicable characters will be converted to HTML entities via PHP's {@link http://php.net/manual/en/function.htmlentities.php htmlentities} function, so remember to use PHP's {@link http://www.php.net/manual/en/function.html-entity-decode.php html_entity_decode} function to do reverse this, if it's the case; If "body" is explicitly disabled via the {@link option} method by setting CURLOPT_NOBODY to 0 or FALSE, this will be an empty string; - response - the response given by the cURL library as an array with 2 entries: the first entry is the textual representation of the result's code, while second is the result's code itself; if the request was successful, these values will be array(CURLE_OK, 0); consult {@link http://www.php.net/manual/en/function.curl-errno.php#103128 this list} to see the possible values of this property; If the callback function returns FALSE while {@link cache} is enabled, the library will not cache the respective request, making it easy to retry failed requests without having to clear all cache. @since 1.3.3 @return void
$callback mixed
return void

queue() public method

Until {@link start} method is called, all calls to {@link delete}, {@link download}, {@link ftp_download}, {@link get}, {@link header}, {@link post} and {@link put} methods will queue up rather than being executed right away. Once the {@link start} method is called, all queued requests will be processed while values of {@link threads} and {@link pause_interval} properties will still apply. 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();
queue requests - useful for grouping different types of requests
in this example, when the "start" method is called, we'll execute
the "get" and the "post" requests simultaneously as if it was a
single request
$curl->queue();
do a POST and execute the "mycallback" function for each
request, as soon as it finishes
$curl->post(array(
    'http://www.somewebsite.com'  =>  array(
        'data_1'  =>  'value 1',
        'data_2'  =>  'value 2',
    ),
), 'mycallback');
let's fetch the RSS feeds of some popular 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')
execute queued requests
$curl->start();
        
Since: 1.3.0 @return void
public queue ( ) : void
return void

scrap() public method

include the Zebra_cURL library require 'path/to/Zebra_cURL'; instantiate the class $curl = new Zebra_cURL(); get page's content only $content = $curl->get('https://www.somewebsite.com/', true); print that to screen echo $content; get everything we can about the page $content = $curl->get('https://www.somewebsite.com/'); print that to screen print_r('
');
print_r($content);
        
public scrap ( string $url, boolean $body_only = false ) : mixed
$url string An URL to fetch @param boolean $body_only (Optional) When set to TRUE, will instruct the method to return only the page's content, without info, headers, responses, etc. When set to FALSE, will instruct the method to return everything it can about the scrapped page, as an object with properties as described for the $callback argument of the {@link get} method. Default is FALSE. @since 1.3.3 @return mixed Returns the scrapped page's content, when $body_only is set to TRUE, or an object with properties as described for the $callback argument of the {@link get} method.
$body_only boolean
return mixed

ssl() public method

include the Zebra_cURL library require 'path/to/Zebra_cURL'; instantiate the class $curl = new Zebra_cURL(); instruct the library to verify peer's SSL certificate (ignored if request is not made through HTTPS) $curl->ssl(true); fetch a page $curl->get('https://www.somewebsite.com/', create_function('$result', 'print_r("
"); print_r($result);'));
        
public ssl ( boolean $verify_peer = true, integer $verify_host = 2, mixed $file = false, mixed $path = false ) : void
$verify_peer boolean (Optional) Should the peer's certificate be verified by cURL? Default is TRUE. This option can also be set using the {@link option} method and setting CURLOPT_SSL_VERIFYPEER option to the desired value. @param integer $verify_host (Optional) Specifies whether or not to check the existence of a common name in the SSL peer certificate and that it matches with the provided hostname. - 1 to check the existence of a common name in the SSL peer certificate; - 2 to check the existence of a common name and also verify that it matches the hostname provided; in production environments the value of this option should be kept at 2; Default is 2 Support for value 1 removed in cURL 7.28.1 This option can also be set using the {@link option} method and setting CURLOPT_SSL_VERIFYHOST option to the desired value. @param mixed $file (Optional) An absolute path to a file holding one or more certificates to verify the peer with. This only makes sense if CURLOPT_SSL_VERIFYPEER is set to TRUE. Default is FALSE. This option can also be set using the {@link option} method and setting CURLOPT_CAINFO option to the desired value. @param mixed $path (Optional) An absolute path to a directory that holds multiple CA certificates. This only makes sense if CURLOPT_SSL_VERIFYPEER is set to TRUE. Default is FALSE. This option can also be set using the {@link option} method and setting CURLOPT_CAPATH option to the desired value. @return void
$verify_host integer
$file mixed
$path mixed
return void

start() public method

See {@link queue} method for more information.
Since: 1.3.0 @return void
public start ( ) : void
return void

Property Details

$pause_interval public_oe property

If the value of this property is greater than 0, the library will process as many requests as defined by the {@link threads} property, and then wait for {@link pause_interval pause_interval} seconds before processing the next batch of requests. Default is 0 (the library will keep as many parallel threads as defined by {@link threads} running at all times, until there are no more requests to process).
Since: 1.3.0 @var integer
public int $pause_interval
return integer

$threads public_oe property

process 30 simultaneous requests, at all times $curl->threads = 30; Note that, unless {@link pause_interval} is set to a value greater than 0, the library will process a constant number of requests, at all times; it's doing this by processing a new request as soon as another one finishes, instead of waiting for each batch to finish, and so on, until there are no more requests to process, and thus greatly decreasing execution time. If {@link pause_interval} is set to a value greater than 0, the library will process as many requests as set by the {@link threads} property and then will wait for {@link pause_interval} seconds before processing the next batch of requests. Default is 10.
public int $threads
return integer