1. Go to this page and download the library: Download gemz/http-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// Authentication
$client->authBasic('username', 'password');
$client->authBearer('token');
// Headers
$client->header('key', 'value');
$client->headers(['key' => 'value']);
// User-Agent
$client->userAgent('userAgent');
// Query Parameters
$client->queryParam('key', 'value');
$client->queryParams(['key1' => 'value1']);
// will result in ...?key=value&key1=value1
// Timeout in seconds
$client->timeout(10);
// Max Redirects
// 0 means unlimited
$client->allowRedirects(3);
$client->doNotAllowRedirects();
// Max Request <-> Response Duration
// in seconds
$client->maxDuration(30);
// Throw errors if response status code is >= 400
$client->throwErrors();
// Content-Types
$client->contentType('contentType');
$client->asJson();
$client->asFormParams();
$client->asPlain();
$client->asMultipart();
// Multipart form data will automatically transformed in the correct format
// throws an exception if content-type and payload does not match
$client->payload(['key' => 'value']);
$client->payload('my message');
// payload for multipart
$client->payload([
'key' => 'value',
'file_field' => Client::fileHandler('pathToFile')
]);
// without verifing ssl
// Default is verifing
$client->doNotVerifySsl();
$client->verifySsl();
// Proxy
$client->useProxy('tcp://...');
// Methods
// Endpoint can also be an URI
$client->get('users/1');
$client->get('https://myapi.com/users/1');
$client->post('users');
$client->put('users/1');
$client->patch('users/1');
$client->head('users/1');
$client->delete('users/1');
// Setting Symfony Specific Client Options
$client->option('<key', '<value');
// can be string, array, object ....
$client->customData(['id' => 182736283]);
// in response
$response->customData(); // outputs ['id' => 182736283]
$response = $client->get('users/1');
// Content Access
$response->asArray(); // if content is json
$response->asObject(); // if content is json
$response->asCollection(); // if content is json - illuminate collection
$response->asString();
$response->asStream(); // php resource
$response->body();
// Status Code
$response->status(); // integer
$response->isOk(); // true / false 200 - 299
$response->isSuccess(); // true / false 200 - 299
$response->isRedirect(); // true / false 300 - 399
$response->isClientError(); // true / false 400 - 499
$response->isServerError(); // true / false 500 - 599
// Headers
$response->header('content-type');
$response->headers();
$response->contentType();
$response->isJson(); // true / false
// Request Url
$response->requestUrl();
// Execution Time request <-> response
$response->executionTime();
// Custom Data
// Data from request custom data
$response->customData();
// response infos
// returns info coming from the transport layer, such as "response_headers",
// "redirect_count", "start_time", "redirect_url", etc.
$response->info();
// symmfony response object
$response->response();
$client = Client::create();
$responses = [];
$responses[] = $client->get('users/1');
$responses[] = $client->get('users/2'));
$responses[] = $client->get('users/3');
// responses are in order of the requests
foreach ($responses as $response) {
echo $response->status());
}
$client = Client::create();
$responses = [];
for ($i = 1; $i < 300; $i++) {
$responses[] = $client
// using custom data to identify the request in the response
->customData('id:' . $i)
->get("users/{$i}");
}
// Using the Stream Object to access the responses
foreach ($client->stream($responses) as $response => $chunk) {
Stream::from($response, $chunk)
->then(function (Response $response) {
// success - do something with the response
echo $response->customData()
})
->timeout(function (Response $response) {
// timeout - do something with the response
})
->catch(function ($exception, Response $response) {
// exception was thrown
});
}
use Gemz\HttpClient\Stream;
foreach ($client->stream($response) as $response => $chunk) {
$stream = Stream::for($response, $chunk);
// fulfilled
// callable must use response
$stream->then(/* callable */);
// timeout
// callable must use response
$stream->timeout(/* callable */);
// rejected
// callable must use exception, response
$stream->catch(/* callable */);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.