use Bayfront\MultiCurl\Client;
$client = new Client();
A base URL can be set so that future request-related methods do not have to specify the full endpoint. For example:
use Bayfront\MultiCurl\Client;
$client = new Client('https://jsonplaceholder.typicode.com');
$response = $client->get('posts/1'); // Endpoint from the base URL
echo $response->getBody();
The cURL handle will be created automatically and be ready to use.
Multiple HTTP requests can be made simultaneously instead of one after the other, thereby limiting the completion time to the duration of the single slowest request instead of the sum of all requests combined.
use Bayfront\MultiCurl\Async;
$async = new Async();
A base URL can be set so that future request-related methods do not have to specify the full endpoint. For example:
use Bayfront\MultiCurl\Async;
use Bayfront\MultiCurl\Exceptions\ClientException;
$async = new Async('https://jsonplaceholder.typicode.com');
$ids = [
'posts',
'comments'
];
$async->create($ids);
try {
$async
->use('posts')->get('posts/1')
->use('comments')->get('comments/1');
} catch (ClientException $e) {
die($e->getMessage());
}
$async->execute();
foreach ($ids as $id) {
try {
echo $async->use($id)->getBody();
} catch (ClientException $e) {
die($e->getMessage());
}
}
cURL handles must be made explicitly using the create()
method before using.
The Bayfront\MultiCurl\Api\InteractsWithApi
trait can be used to simplify making asynchronous requests to an API.
For more information, see API documentation.
Async class only
Once a cURL handle has been created, the following methods can be used:
Request
Response
- getHeaders
- getHeader
- getBody
- getErrorNumber
- isError
- getErrorMessage
- getStatusCode
- getInfo
- isInformational
- isSuccessful
- isRedirection
- isClientError
- isServerError
- isOk
- isForbidden
- isNotFound
Description:
(Only available in Async
class)
Create cURL handles with identifiers.
cURL handles must be created before they can be used.
Parameters:
$ids
(array)
Returns:
- (self)
Example:
use Bayfront\MultiCurl\Async;
$async = new Async('https://jsonplaceholder.typicode.com');
$ids = [
'posts',
'comments'
];
$async->create($ids);
Description:
(Only available in Async
class)
Sets current cURL handle.
Once the cURL handle has been created using create()
, it can be used by specifying the ID of the handle you wish to use.
A Bayfront\MultiCurl\Exceptions\ClientException
exception will be thrown if the ID does not exist.
Parameters:
$id
(string)
Returns:
- (self)
Throws:
Bayfront\MultiCurl\Exceptions\ClientException
Example:
use Bayfront\MultiCurl\Async;
use Bayfront\MultiCurl\Exceptions\ClientException;
$async = new Async('https://jsonplaceholder.typicode.com');
$ids = [
'posts',
'comments'
];
$async->create($ids);
try {
$async
->use('posts')->get('posts/1')
->use('comments')->get('comments/1');
} catch (ClientException $e) {
die($e->getMessage());
}
$async->execute();
Description:
(Only available in Async
class)
Execute the given cURL session.
The response methods will only return results after the execute()
method has been called.
Parameters:
- None
Returns:
- (self)
Example:
See the above example for use().
Description:
Returns the cURL handle.
A Bayfront\MultiCurl\Exceptions\ClientException
exception will be thrown if the handle does not exist.
Parameters:
- None
Returns:
- (resource): cURL handle
Throws:
Bayfront\MultiCurl\Exceptions\ClientException
Description:
Resets all request settings.
Parameters:
- None
Returns:
- (self)
Description:
Reset all settings and close the cURL handle(s).
NOTE: This method is called in the class destructor.
Parameters:
- None
Returns:
- (self)
Description:
Sets an array of options for the cURL session.
Parameters:
$options
(array)
Returns:
- (self)
Example:
$client->setOptions([
CURLOPT_HEADER => false
]);
Description:
Sets an array of headers for the cURL session.
Parameters:
$headers
(array)
Returns:
- (self)
Example:
$client->setHeaders([
'Content-Type' => 'application/json; charset=UTF-8'
]);
Description:
Sets token authorization header using a given token.
Parameters:
$token
(string)
Returns:
- (self)
Description:
(Only available in Client
class)
Initiates file download in the browser.
Parameters:
$url
(string)$memory_limit = 128
(int): Memory limit (in MB)
Returns:
- (void)
Example:
$client = new Client();
$client->download('https://www.example.com/image.jpg');
Description:
Executes GET
request, including optional data sent as query parameters.
Parameters:
$url
(string)$data = []
(array)
Returns:
- (self)
Example:
$client = new Client('https://jsonplaceholder.typicode.com');
$response = $client->get('posts/1');
Description:
Executes CONNECT
request, including optional data.
Parameters:
$url
(string)$data = []
(array)$json_encode = false
(bool): json_encode the$data
array and set theContent-Type
header asapplication/json
, if not already defined
Returns:
- (self)
Description:
Executes DELETE
request, including optional data.
Parameters:
$url
(string)$data = []
(array)$json_encode = false
(bool): json_encode the$data
array and set theContent-Type
header asapplication/json
, if not already defined
Returns:
- (self)
Description:
Executes HEAD
request, including optional data.
Parameters:
$url
(string)$data = []
(array)$json_encode = false
(bool): json_encode the$data
array and set theContent-Type
header asapplication/json
, if not already defined
Returns:
- (self)
Description:
Executes OPTIONS
request, including optional data.
Parameters:
$url
(string)$data = []
(array)$json_encode = false
(bool): json_encode the$data
array and set theContent-Type
header asapplication/json
, if not already defined
Returns:
- (self)
Description:
Executes PATCH
request, including optional data.
Parameters:
$url
(string)$data = []
(array)$json_encode = false
(bool): json_encode the$data
array and set theContent-Type
header asapplication/json
, if not already defined
Returns:
- (self)
Description:
Executes POST
request, including optional data.
Parameters:
$url
(string)$data = []
(array)$json_encode = false
(bool): json_encode the$data
array and set theContent-Type
header asapplication/json
, if not already defined
Returns:
- (self)
Description:
Executes PUT
request, including optional data.
Parameters:
$url
(string)$data = []
(array)$json_encode = false
(bool): json_encode the$data
array and set theContent-Type
header asapplication/json
, if not already defined
Returns:
- (self)
Description:
Executes TRACE
request, including optional data.
Parameters:
$url
(string)$data = []
(array)$json_encode = false
(bool): json_encode the$data
array and set theContent-Type
header asapplication/json
, if not already defined
Returns:
- (self)
Description:
Returns array of headers from the previous request.
Parameters:
- None
Returns:
- (array)
Example:
$client = new Client('https://jsonplaceholder.typicode.com');
$response = $client->get('posts/1');
print_r($response->getHeaders());
Description:
Returns single header value from the previous request, with optional default value if not existing.
Parameters:
$header
(string)$default = NULL
(mixed)
Returns:
- (mixed)
Example:
$client = new Client('https://jsonplaceholder.typicode.com');
$response = $client->get('posts/1');
echo $response->getHeader('Date');
Description:
Returns body of the previous request, with optional default value if not existing.
Parameters:
$json_decode = false
(bool): Decode JSON contents to an array$default = NULL
(mixed)
Returns:
- (mixed)
Example:
$client = new Client('https://jsonplaceholder.typicode.com');
$response = $client->get('posts/1');
echo $response->getBody();
Description:
Returns error number of the previous request, or zero if no error exists.
Parameters:
- None
Returns:
- (int)
Example:
$client = new Client('https://jsonplaceholder.typicode.com');
$response = $client->get('posts/1');
echo $response->getErrorNumber();
Description:
Is previous request an error.
Parameters:
- None
Returns:
- (bool)
Description:
Returns error message of the previous request, or an empty string if no error occurred.
Parameters:
- None
Returns:
- (string)
Description:
Returns status code of the previous request, or zero if not existing.
Parameters:
- None
Returns:
- (int)
Description:
Returns array of information about the previous request, a single option constant, or null if not existing.
Parameters:
$opt = NULL
(mixed): Optional option constant
For more information, see: curl_getinfo
Returns:
- (mixed)
Description:
Is status code informational.
Parameters:
- None
Returns:
- (bool)
Description:
Is status code successful.
Parameters:
- None
Returns:
- (bool)
Description:
Is status code a redirection.
Parameters:
- None
Returns:
- (bool)
Description:
Is status code a client error.
Parameters:
- None
Returns:
- (bool)
Description:
Is status code a server error.
Parameters:
- None
Returns:
- (bool)
Description:
Is status code OK (200).
Parameters:
- None
Returns:
- (bool)
Description:
Is status code forbidden (403).
Parameters:
- None
Returns:
- (bool)
Description:
Is status code not found (404).
Parameters:
- None
Returns:
- (bool)