PHP code example of guanhui07 / guzzle

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

    

guanhui07 / guzzle example snippets




aylin666\Guzzle\Client;
use Raylin666\Pool\PoolOption;

/***********************************************
 * 非常驻内存环境下使用方式 (非Swoole) 
 ***********************************************/

$client = new Client();
$client = $client->create();
var_dump($client->post('http://127.0.0.1:9902/api/v1/login', [
    'form_params' => [
        'username' => 'raylin',
        'password' => '123456',
    ]
])->getBody()->getContents());

/**
 * 输出:
 *      string(293) "{"code":200,"data":{"expire_at":1615472981,"id":1,"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySUQiOjEsImV4cCI6MTYxNTQ3Mjk4MSwiaWF0IjoxNjE1Mzg2NTgxLCJpc3MiOiJnaW4tYXBpIiwibmJmIjoxNjE1Mzg2NTgxfQ.4d622SGpzldippeBaoKhXI29V6zVyflZST0coMwpWeg"},"message":"OK","responseTime":"66.880505ms"}"
 */

/***********************************************
 * 常驻内存环境下使用方式 (Swoole, 协程) 
 ***********************************************/

$server = new swoole_http_server('127.0.0.1', 9998);

$server->set([
    'worker_num' => swoole_cpu_num(),
]);

// 如果您需要使用 $container , 请自行 composer 

// on worker start
$client = new Client();
$client->withPoolOption(
    (new PoolOption())->withMinConnections(1)
        ->withMaxConnections(10)
        ->withWaitTimeout(10)
);
$container->make(\GuzzleHttp\Client::class, [function () use ($client) {
    return $client->create();
}]);
        
        
        
  // 控制器中使用 
 $client = di()->get(\GuzzleHttp\Client::class);
$result = $client->get('http://baidu.com');
var_dump($result->getBody()->getContents());
$result->getBody()->close();