A queue based curl wrapping class. Processes curl GET and POST requests from a queue in batches. Output is handled by callback functions which can be set globally for all requests or individually for each request.
Example usage can be found in example.php.
- Efficient queue based processing of HTTP requests.
- Easy to use interface.
- Customizeable window size defines number of requests that are processed in parallel at any one time.
- Ability to define per request or global output callback functions.
- Ability to customize the curl options for each request.
- Ability to choose either GET or POST for each request.
Config values can be set using the config($values) where $values is an array in the form of $values = array($key => $value).
windowsets how many requests will be processed at the same timetimeoutsets the maximum timeout in secondsoptionsis an array of additional default CURLOPT$key => $valuepairs pass to each Curl requestcallbackspecifies the default function to call when a request finishes, must be of type function($output,$info)
CurlQueue offers 3 ways to add new requests to the waiting queue. In all cases, the only required parameter is $url. If you have not defined a default callback function via the config() function then a callback must be included with each request.
get($url,$headers,$options,$callback)post($url,$post_data,$headers,$options,$callback)request($url,$method,$post_data,$headers,$options,$callback)
Depending on the type of request being created, there are 4-6 parameters available to further customize the request.
$urlis a string that contains the URL that should be queried.$methodis a string that specifies what type of request this should be - 'GET' or 'POST'.$post_datacontains the CURLOPT_POSTFIELDS formatted data to be sent to the remote host.$headersis an array of CURLOPT_HTTPHEADER formatted header fields to be sent to the remote host.$optionsis an array of additional CURLOPT$key => $valuepairs to further customize each request.$callbackis the function to be called, when data has been returned, in the PHP callable format. See below for more details.
CurlQueue returns data from each request through a user defined callback function. A default callback function can be set via the config() function or a callback can be included with each request. Callbacks included with a request overrite the default callback, if one exists.
Functions must be formatted according to the PHP callable type specifications (http://php.net/manual/en/language.types.callable.php). Typically, callback functions will be formatted in one of the following ways.
- Static Function:
$callback = "myfunctionname" - Static Class Method:
$callback = array("myclass","method_name_in_myclass") - Static Class Method:
$callback = "myclass::method_name_in_myclass" - Object Method:
$callback = array($myclass,"method_name_in_myclass")
The provided callback must have two parameters - $output and $info, in that order. $output contains the raw output from the curl request and $info contains an array of information about the request from Curl.
Please note that a callback is optional when only a single request has been added. The $output will be returned via the execute() function in this case.
Once all requests have been created and are ready to be executed, you should call the execute() method. This method will return one of the following.
TRUEreturned when all requests have been processed succedfully.FALSEreturned when all requests have NOT been processed, possibly because of bad config values.$outputis the raw output from a single request. This will only be returned when a single request has been added.
Copyright 2011 Matthew Colf mattcolf@mattcolf.com
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.