Access all of Typerocket. Get Pro.
cURL Client
( v6 )
- # Getting Started
- # All Shorthand Methods
- # Custom Instance
- # Headers
- # Data
- # Auth
- # URL
- # Get Curl Resource
- # Execute
Pro Only Feature
Getting Started
IMPORTANT: You must have the PHP cURL extension installed to use this class.
TypeRocket provides a simple cURL Http client. Take a look at sending a POST request.
$url = 'https://exmaple.com';
$data = [
'app' => 'TypeRocket',
'downlaods' => '10000',
];
$json = true; // Make request as JSON
$http = \TypeRocket\Pro\Utility\Http::post($url, $data, $json);
$returned = $http->exec();
All Shorthand Methods
\TypeRocket\Pro\Utility\Http::get($url);
\TypeRocket\Pro\Utility\Http::post($url, $data, $json);
\TypeRocket\Pro\Utility\Http::put($url, $data, $json);
\TypeRocket\Pro\Utility\Http::delete($url, $data, $json);
Custom Instance
$method = 'GET'; // Any valid HTTP method can be used
$http = new \TypeRocket\Pro\Utility\Http($url, $method);
Headers
You can add headers:
$http->headers([
'Content-Type' => 'application/json',
'X-Custom' => 'value'
]);
Data
You can override the data sent:
$json = true; // Make request as JSON
$http->data(['my' => 'data'], $json);
Auth
Add basic HTTP authentication.
$http->auth($username, $password);
URL
A URL will always be present. But you can set a new one.
$http->url('https://example.com');
Get Curl Resource
Get, or override, the reference to the curl resource.
$http->curl();
Inject an existing curl resource into the class and override the existing one.
$curl = curl_init();
$url = 'https://example.com';
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
// Close the existing curl request
$http = (new \TypeRocket\Pro\Utility\Http($url))->curl(null);
// Override
$http->curl($curl);
Execute
Execute the curl request and get the returned value.
$json = true; // Make request as JSON
$returned = $http->exec();
The returned value is a TypeRocket\Pro\Utility\CurlResponse
object.
Found a typo? Something is wrong in this documentation? Fork and edit it!