PHP Class RollingCurl\RollingCurl

Show file Open project: chuyskywalker/rolling-curl Class Usage Examples

Protected Properties

Property Type Description
$multicurlOptions array Set your default multicurl options
$options array Set your base options that you want to be used with EVERY request. (Can be overridden individually)

Public Methods

Method Description
add ( RollingCurl\Request $request ) : RollingCurl Add a request to the request queue
addMulticurlOptions ( array $multicurlOptions ) : RollingCurl Override and add multicurlOptions
addOptions ( array $options ) : RollingCurl Override and add options
clearCompleted ( ) : RollingCurl Clear out all completed requests
countActive ( ) : integer
countCompleted ( boolean $useArray = false ) : integer
countPending ( ) : integer
delete ( string $url, array $headers = null, array $options = null ) : RollingCurl Perform DELETE request
execute ( ) : void Run all queued requests
get ( string $url, array $headers = null, array $options = null ) : RollingCurl Perform GET request
getCallback ( ) : callable
getCompletedRequests ( ) : RollingCurl\Request[]
getHeaders ( ) : array
getIdleCallback ( ) : callable
getMulticurlOptions ( ) : array
getOptions ( ) : array
getSimultaneousLimit ( ) : integer
post ( string $url, array | string $postData = null, array $headers = null, array $options = null ) : RollingCurl Perform POST request
prunePendingRequestQueue ( ) : RollingCurl Removes requests from the queue that have already been processed
put ( string $url, null $putData = null, array $headers = null, array $options = null ) : RollingCurl Perform PUT request
request ( string $url, string $method = "GET", array | string $postData = null, array $headers = null, array $options = null ) : RollingCurl Create new Request and add it to the request queue
setCallback ( callable $callback ) : RollingCurl Define a callable to handle the response.
setHeaders ( array $headers ) : RollingCurl
setIdleCallback ( callable $callback ) : RollingCurl Define a callable to be called when waiting for responses.
setMulticurlOptions ( array $multicurlOptions ) : RollingCurl
setOptions ( array $options ) : RollingCurl
setSimultaneousLimit ( integer $count ) : RollingCurl Set the limit for how many cURL requests will be execute simultaneously.

Private Methods

Method Description
getNextPendingRequest ( ) : null | RollingCurl\Request Get the next pending request, or return null
getNextPendingRequests ( integer $limit = 1 ) : RollingCurl\Request[] Return the next $limit pending requests (may return an empty array)
prepareRequestOptions ( RollingCurl\Request $request ) : array Helper function to gather all the curl options: global, inferred, and per request

Method Details

add() public method

Add a request to the request queue
public add ( RollingCurl\Request $request ) : RollingCurl
$request RollingCurl\Request
return RollingCurl

addMulticurlOptions() public method

Override and add multicurlOptions
public addMulticurlOptions ( array $multicurlOptions ) : RollingCurl
$multicurlOptions array
return RollingCurl

addOptions() public method

Override and add options
public addOptions ( array $options ) : RollingCurl
$options array
return RollingCurl

clearCompleted() public method

If you are running a very large number of requests, it's a good idea to call this every few completed requests so you don't run out of memory.
public clearCompleted ( ) : RollingCurl
return RollingCurl

countActive() public method

public countActive ( ) : integer
return integer

countCompleted() public method

public countCompleted ( boolean $useArray = false ) : integer
$useArray boolean count the completedRequests array is true. Otherwise use the global counter.
return integer

countPending() public method

public countPending ( ) : integer
return integer

delete() public method

Perform DELETE request
public delete ( string $url, array $headers = null, array $options = null ) : RollingCurl
$url string
$headers array
$options array
return RollingCurl

execute() public method

Run all queued requests
public execute ( ) : void
return void

get() public method

Perform GET request
public get ( string $url, array $headers = null, array $options = null ) : RollingCurl
$url string
$headers array
$options array
return RollingCurl

getCallback() public method

public getCallback ( ) : callable
return callable

getCompletedRequests() public method

public getCompletedRequests ( ) : RollingCurl\Request[]
return RollingCurl\Request[]

getHeaders() public method

public getHeaders ( ) : array
return array

getIdleCallback() public method

public getIdleCallback ( ) : callable
return callable

getMulticurlOptions() public method

public getMulticurlOptions ( ) : array
return array

getOptions() public method

public getOptions ( ) : array
return array

getSimultaneousLimit() public method

public getSimultaneousLimit ( ) : integer
return integer

post() public method

Perform POST request
public post ( string $url, array | string $postData = null, array $headers = null, array $options = null ) : RollingCurl
$url string
$postData array | string
$headers array
$options array
return RollingCurl

prunePendingRequestQueue() public method

Beceause the request queue does not shrink during processing (merely traversed), it is sometimes necessary to prune the queue. This method creates a new array starting at the first un-processed request, replaces the old queue and resets counters.
public prunePendingRequestQueue ( ) : RollingCurl
return RollingCurl

put() public method

Perform PUT request
public put ( string $url, null $putData = null, array $headers = null, array $options = null ) : RollingCurl
$url string
$putData null
$headers array
$options array
return RollingCurl

request() public method

Create new Request and add it to the request queue
public request ( string $url, string $method = "GET", array | string $postData = null, array $headers = null, array $options = null ) : RollingCurl
$url string
$method string
$postData array | string
$headers array
$options array
return RollingCurl

setCallback() public method

It can be an anonymous function: $rc = new RollingCurl(); $rc->setCallback(function($request, $rolling_curl) { process }); Or an existing function: class MyClass { function doCurl() { $rc = new RollingCurl(); $rc->setCallback(array($this, 'callback')); } Cannot be private or protected public function callback($request, $rolling_curl) { process } } The called code should expect two parameters: \RollingCurl\Request $request, \RollingCurl\RollingCurl $rollingCurl $request is original request object, but now with body, headers, response code, etc $rollingCurl is the rolling curl object itself (useful if you want to re/queue a URL)
public setCallback ( callable $callback ) : RollingCurl
$callback callable
return RollingCurl

setHeaders() public method

public setHeaders ( array $headers ) : RollingCurl
$headers array
return RollingCurl

setIdleCallback() public method

Define a callable to be called when waiting for responses.
public setIdleCallback ( callable $callback ) : RollingCurl
$callback callable
return RollingCurl

setMulticurlOptions() public method

public setMulticurlOptions ( array $multicurlOptions ) : RollingCurl
$multicurlOptions array
return RollingCurl

setOptions() public method

public setOptions ( array $options ) : RollingCurl
$options array
return RollingCurl

setSimultaneousLimit() public method

Please be mindful that if you set this too high, requests are likely to fail more frequently or automated software may perceive you as a DOS attack and automatically block further requests.
public setSimultaneousLimit ( integer $count ) : RollingCurl
$count integer
return RollingCurl

Property Details

$multicurlOptions protected property

Set your default multicurl options
protected array $multicurlOptions
return array

$options protected property

Set your base options that you want to be used with EVERY request. (Can be overridden individually)
protected array $options
return array