PHP code example of php-packagist / rocket

1. Go to this page and download the library: Download php-packagist/rocket 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/ */

    

php-packagist / rocket example snippets




use PhpPackagist\Rocket\Rocket;

$response = Rocket::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 Rocket::get('http://test.com/users/1')['name'];



$response = Rocket::post('http://test.com/users', [
    'name' => 'Steve',
    'role' => 'Network Administrator',
]);



$response = Rocket::asForm()->post('http://test.com/users', [
    'name' => 'Sara',
    'role' => 'Privacy Consultant',
]);



$response = Rocket::attach(
    'attachment', file_get_contents('photo.jpg'), 'photo.jpg'
)->post('http://test.com/attachments');



$photo = fopen('photo.jpg', 'r');

$response = Rocket::attach(
    'attachment', $photo, 'photo.jpg'
)->post('http://test.com/attachments');



$response = Rocket::withHeaders([
    'X-First' => 'foo',
    'X-Second' => 'bar'
])->post('http://test.com/users', [
    'name' => 'Taylor',
]);



// Basic 认证...
$response = Rocket::withBasicAuth('[email protected]', 'secret')->post(...);

// Digest 认证...
$response = Rocket::withDigestAuth('[email protected]', 'secret')->post(...);



$response = Rocket::withToken('token')->post(...);



$response = Rocket::retry(3, 100)->post(...);



// 确认状态码是否在 200 到 300 之间(包含 200)
$response->successful();

// 确认是否发生了 400 级别的错误(以 4 开头的状态码)
$response->clientError();

// 确认是否发生了 500 级别的错误(以 5 开头的状态码)
$response->serverError();



$response = Rocket::post(...);

// 在客户端或服务端错误发生时抛出异常
$response->throw();

return $response['user']['id'];



return Trpc::post(...)->throw()->json();

composer