private function _queue_requests()
{
// get the number of remaining urls
$requests_count = count($this->_requests);
// iterate through the items in the queue
for ($i = 0; $i < ($requests_count < $this->threads ? $requests_count : $this->threads); $i++) {
// remove the first request from the queue
$request = array_shift($this->_requests);
// initialize individual cURL handle with the URL
$handle = curl_init($request['url']);
// get the handle's ID
$resource_number = preg_replace('/Resource id #/', '', $handle);
// if we're downloading something
if (isset($request['options'][CURLOPT_BINARYTRANSFER]) && $request['options'][CURLOPT_BINARYTRANSFER]) {
// use parse_url to analyze the string
// we use this so we won't have hashtags and/or query string in the file's name later on
$parsed = parse_url($request['url']);
// open a file and save the file pointer
$request['file_handler'] = fopen($request['path'] . basename($parsed['path']), 'w+');
// tell libcurl to use the file for streaming the download
$this->option(CURLOPT_FILE, $request['file_handler']);
}
// set request's options
foreach ($request['options'] as $key => $value) {
$this->option($key, $value);
}
// in some cases, CURLOPT_HTTPAUTH and CURLOPT_USERPWD need to be set as last options in order to work
$options_to_be_set_last = array(10005, 107);
// iterate through all the options
foreach ($this->options as $key => $value) {
// if this option is one of those to be set at the end
if (in_array($key, $options_to_be_set_last)) {
// remove the option from where it is
unset($this->options[$key]);
// add option at the end
$this->options[$key] = $value;
}
}
// set options for the handle
curl_setopt_array($handle, $this->options);
// add the normal handle to the multi handle
curl_multi_add_handle($this->_multi_handle, $handle);
// add request to the list of running requests
$this->_running['fh' . $resource_number] = $request;
}
}