Skip to content

Latest commit

 

History

History
111 lines (76 loc) · 2.03 KB

curl-client.md

File metadata and controls

111 lines (76 loc) · 2.03 KB

Title: cURL Client Description: A simple cURL client.


!!

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 = \TypeRocketPro\Utility\Http::post($url, $data, $json);
$returned = $http->exec();

All Shorthand Methods

\TypeRocketPro\Utility\Http::get($url);
\TypeRocketPro\Utility\Http::post($url, $data, $json);
\TypeRocketPro\Utility\Http::put($url, $data, $json);
\TypeRocketPro\Utility\Http::delete($url, $data, $json);

Custom Instance

$method = 'GET'; // Any valid HTTP method can be used
$http = new \TypeRocketPro\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 \TypeRocketPro\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 TypeRocketPro\Utility\CurlResponse object.