PHP code example of babytree-com / httpclient

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

    

babytree-com / httpclient example snippets


use BPF\HttpClient\Psr\RequestOptions;
use BPF\HttpClient\RequestClient;

$options = array(
    // some options
);
$request_uniq = $request_client->addRequest($some_api, $options, RequestClient::MODE_ASYNC);
$ret = $request_client->getResponse($request_uniq);

// 对请求结果进行业务操作
// ...


use BPF\HttpClient\Psr\RequestOptions;
use BPF\HttpClient\RequestClient;

$options = array(
    // some options
);
$request_uniq = $request_client->addRequest($some_api, $options, RequestClient::MODE_ASYNC);

// 这里可以放可以和请求并行处理的业务逻辑 
// some business code

$ret = $request_client->getResponse($request_uniq);

// 对请求结果进行业务操作
// ...

use BPF\HttpClient\Psr\RequestOptions;
use BPF\HttpClient\RequestClient;

$request_client = new RequestClient();
$options = array(
    // some options
);
$multi_urls = array(
            $api1,
            $api2,
            $api3,
            ...
        );
$request_list = array();
foreach ($multi_urls as $url) {
    $request_uniq = $request_client->addRequest($url, $options, RequestClient::MODE_ASYNC);
    $request_list[$request_uniq] = $request_uniq;
}

// 这里可以放可以和请求并行处理的业务逻辑 
// some business code

do {
    $request_uniq = null;
    try {
        $ret = $request_client->selectGetAsyncResponse($request_uniq, null);
    } catch (\Exception $e) {
    }
    if ($request_uniq && isset($request_list[$request_uniq])) {
        unset($request_list[$request_uniq]);
    }
    if (!$request_list) {
        break;
    }
} while (true);

// 对请求结果进行业务操作
// ...