PHP code example of azurre / php-http-client
1. Go to this page and download the library: Download azurre/php-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/ */
azurre / php-http-client example snippets
use \Azurre\Component\Http\Client;
$request = Client::create()->get('http://example.com/')->execute();
echo $request->getResponse();
use \Azurre\Component\Http\Client;
$data = [
'key' => 'some key',
'comment' => 'Some string'
];
try {
$request = Client::create()->post('http://example.com/', $data)->execute();
} catch (\Exception $e) {
echo $e->getMessage();
}
var_dump($request->getStatusCode());
var_dump($request->getResponseCookies());
var_dump($request->getResponseHeaders());
echo $request->getResponse();
use \Azurre\Component\Http\Client;
$data = [
'string' => 'Some string',
'numbers' => [1, 2, 3]
];
try {
$request = Client::create();
$request
->post('http://example.com/', $data)
->setProxy('tcp://192.168.0.2:3128')
->setHeader('X-SECRET-TOKEN', 'f36d8f0f53aefb121531567849')
->setCookies('cookieName','cookieValue')
->verifySSL(false) // Accept sef-signed certificates
->setTimeout(10)
->setIsJson()
->execute();
} catch (\Exception $e) {
echo $e->getMessage();
}
$progressCallback = function ($downloaded, $total) {
if ($total) {
$percent = round(($downloaded / $total) * 100, 2);
echo "Downloaded: $downloaded / $total bytes ($percent%)\r\n";
} else {
echo "Downloaded: $downloaded bytes\r\n";
}
};
$dst = sys_get_temp_dir() . DIRECTORY_SEPARATOR . '__' . time() . '_test.mp4';
//$testUrl = 'https://freetestdata.com/wp-content/uploads/2022/02/Free_Test_Data_1MB_MP4.mp4';
$testUrl = 'https://freetestdata.com/wp-content/uploads/2022/02/Free_Test_Data_10MB_MP4.mp4';
$time = microtime(true);
$request1 = Client::create()->download($testUrl, $dst, $progressCallback);
//$request2 = Client::create()->get($testUrl)->execute();
$duration = round(microtime(true) - $time, 2);
$memory = round(memory_get_peak_usage() / (1024 * 1024), 2);
echo PHP_EOL;
echo "Memory usage: {$memory} Mb" . PHP_EOL;
echo "Duration: {$duration} sec" . PHP_EOL;