PHP code example of flc / http
1. Go to this page and download the library: Download flc/http 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/ */
flc / http example snippets
use Flc\Http\Client;
$response = Client::get('http://test.com');
$response->body() : string;
$response->json() : array;
$response->status() : int;
$response->ok() : bool;
$response->successful() : bool;
$response->serverError() : bool;
$response->clientError() : bool;
$response->header($header) : string;
$response->headers() : array;
return Client::get('http://test.com/users/1')['name'];
$response = Client::post('http://test.com/users', [
'name' => 'Steve',
'role' => 'Network Administrator',
]);
$response = Client::asForm()->post('http://test.com/users', [
'name' => 'Sara',
'role' => 'Privacy Consultant',
]);
$response = Client::attach(
'attachment', file_get_contents('photo.jpg'), 'photo.jpg'
)->post('http://test.com/attachments');
$photo = fopen('photo.jpg', 'r');
$response = Client::attach(
'attachment', $photo, 'photo.jpg'
)->post('http://test.com/attachments');
$response = Client::withHeaders([
'X-First' => 'foo',
'X-Second' => 'bar'
])->post('http://test.com/users', [
'name' => 'Taylor',
]);
// Basic 认证...
$response = Client::withBasicAuth('[email protected] ', 'secret')->post(...);
// Digest 认证...
$response = Client::withDigestAuth('[email protected] ', 'secret')->post(...);
$response = Client::withToken('token')->post(...);
$response = Client::retry(3, 100)->post(...);
// 确认状态码是否在 200 到 300 之间(包含 200)
$response->successful();
// 确认是否发生了 400 级别的错误(以 4 开头的状态码)
$response->clientError();
// 确认是否发生了 500 级别的错误(以 5 开头的状态码)
$response->serverError();
$response = Client::post(...);
// 在客户端或服务端错误发生时抛出异常
$response->throw();
return $response['user']['id'];
return Client::post(...)->throw()->json();