Zebra_cURL::download PHP Method

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
    public function download($urls, $path, $callback = '')
    {
        // if destination path is not a directory or is not writable, trigger an error message
        if (!is_dir($path) || !is_writable($path)) {
            trigger_error('"' . $path . '" is not a valid path or is not writable', E_USER_ERROR);
        }
        // iterate through the list of URLs to process
        foreach ((array) $urls as $url) {
            // add each URL and associated properties to the "_requests" property
            $this->_requests[] = array('url' => $url, 'path' => rtrim($path, '/\\') . '/', 'options' => array(CURLINFO_HEADER_OUT => 1, CURLOPT_BINARYTRANSFER => 1, CURLOPT_HEADER => 0, CURLOPT_CUSTOMREQUEST => null, CURLOPT_HTTPGET => null, CURLOPT_NOBODY => null, CURLOPT_POST => null, CURLOPT_POSTFIELDS => null), 'callback' => $callback, 'arguments' => array_slice(func_get_args(), 3));
        }
        // if we're just queuing requests for now, do not execute the next lines
        if ($this->_queue) {
            return;
        }
        // if we have to pause between batches of requests, process them sequentially, in batches
        if ($this->pause_interval > 0) {
            $this->_process_paused();
        } else {
            $this->_process();
        }
    }

Usage Example

Esempio n. 1
0
 function __construct()
 {
     if (!file_exists(BASE_PATH . '/assets/data/CLDR/en.xml') || !file_exists(BASE_PATH . '/assets/data/CLDR/supplementalData.xml')) {
         $curl = new Zebra_cURL();
         $curl->download(array('http://www.unicode.org/repos/cldr/tags/latest/common/main/en.xml', 'http://unicode.org/repos/cldr/trunk/common/supplemental/supplementalData.xml'), BASE_PATH . '/assets/data/cldr/');
     }
     $this->main = simplexml_load_file(BASE_PATH . '/assets/data/CLDR/en.xml', 'SimpleXMLElement', LIBXML_COMPACT);
     $this->supplemental = simplexml_load_file(BASE_PATH . '/assets/data/CLDR/supplementalData.xml', 'SimpleXMLElement', LIBXML_COMPACT);
     $this->territoryContainment = array('001' => array('019' => array('021' => array('BM', 'CA', 'GL', 'PM', 'US'), '013' => array('BZ', 'CR', 'GT', 'HN', 'MX', 'NI', 'PA', 'SV'), '029' => array('AG', 'AI', 'AW', 'BB', 'BL', 'BQ', 'BS', 'CU', 'CW', 'DM', 'DO', 'GD', 'GP', 'HT', 'JM', 'KN', 'KY', 'LC', 'MF', 'MQ', 'MS', 'PR', 'SX', 'TC', 'TT', 'VC', 'VG', 'VI'), '005' => array('AR', 'BO', 'BR', 'CL', 'CO', 'EC', 'FK', 'GF', 'GY', 'PE', 'PY', 'SR', 'UY', 'VE')), '002' => array('015' => array('DZ', 'EG', 'EH', 'LY', 'MA', 'SD', 'SS', 'TN', 'EA', 'IC'), '011' => array('BF', 'BJ', 'CI', 'CV', 'GH', 'GM', 'GN', 'GW', 'LR', 'ML', 'MR', 'NE', 'NG', 'SH', 'SL', 'SN', 'TG'), '017' => array('AO', 'CD', 'CF', 'CG', 'CM', 'GA', 'GQ', 'ST', 'TD'), '014' => array('BI', 'DJ', 'ER', 'ET', 'KE', 'KM', 'MG', 'MU', 'MW', 'MZ', 'RE', 'RW', 'SC', 'SO', 'TZ', 'UG', 'YT', 'ZM', 'ZW'), '018' => array('BW', 'LS', 'NA', 'SZ', 'ZA')), '150' => array('154' => array('GG', 'IM', 'JE', 'AX', 'DK', 'EE', 'FI', 'FO', 'GB', 'IE', 'IS', 'LT', 'LV', 'NO', 'SE', 'SJ'), '155' => array('AT', 'BE', 'CH', 'DE', 'FR', 'LI', 'LU', 'MC', 'NL'), '151' => array('BG', 'BY', 'CZ', 'HU', 'MD', 'PL', 'RO', 'RU', 'SK', 'UA'), '039' => array('AD', 'AL', 'BA', 'ES', 'GI', 'GR', 'HR', 'IT', 'ME', 'MK', 'MT', 'RS', 'PT', 'SI', 'SM', 'VA', 'XK')), '142' => array('145' => array('AE', 'AM', 'AZ', 'BH', 'CY', 'GE', 'IL', 'IQ', 'JO', 'KW', 'LB', 'OM', 'PS', 'QA', 'SA', 'SY', 'TR', 'YE'), '143' => array('TM', 'TJ', 'KG', 'KZ', 'UZ'), '030' => array('CN', 'HK', 'JP', 'KP', 'KR', 'MN', 'MO', 'TW'), '034' => array('AF', 'BD', 'BT', 'IN', 'IR', 'LK', 'MV', 'NP', 'PK'), '035' => array('BN', 'ID', 'KH', 'LA', 'MM', 'MY', 'PH', 'SG', 'TH', 'TL', 'VN')), '009' => array('053' => array('AU', 'NF', 'NZ'), '054' => array('FJ', 'NC', 'PG', 'SB', 'VU'), '057' => array('FM', 'GU', 'KI', 'MH', 'MP', 'NR', 'PW'), '061' => array('AS', 'CK', 'NU', 'PF', 'PN', 'TK', 'TO', 'TV', 'WF', 'WS'), 'QO' => array('AQ', 'BV', 'CC', 'CX', 'GS', 'HM', 'IO', 'TF', 'UM', 'AC', 'CP', 'DG', 'TA'))));
 }
All Usage Examples Of Zebra_cURL::download