PHP code example of babytree / httpclient

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


use Babytree\HttpClient\Psr\RequestOptions;
use Babytree\HttpClient\RequestClient;

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

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


use Babytree\HttpClient\Psr\RequestOptions;
use Babytree\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 Babytree\HttpClient\Psr\RequestOptions;
use Babytree\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);

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

$options = array(
	//请求超时时间
	RequestOptions::TIMEOUT => 3,
	//debug, $stream不指定时输出到标准设备
	RequestOptions::DEBUG  => $stream,
	//设置header
	RequestOptions::HEADERS => [
	        'timestamp'    => time() * 1000,
	        'signature'    => $signature,
	        'platform'     => 1,
	        'token'        => $meitun_token,
	    ],
	//代理
	RequestOptions::PROXY   => '172.16.99.239:8888',
	//post json格式 默认添加header 'Content-Type', 'application/json;charset=utf-8'
	RequestOptions::JSON => array(
	    'baby_id' => '11111',
	    'baby_name' => '对对对',
	    'baby_gender' => '男',
	    ),
	//post form表单格式 默认添加header 'Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'
	RequestOptions::FORM_PARAMS => array(
	    'baby_id' => '11111',
	    'baby_name' => '对对对',
	    'baby_gender' => '男',
	    ),
	//post上传文件 默认添加header 'Content-Type', 'multipart/form-data; boundary='
	RequestOptions::MULTIPART => array(
	    'id'        => 1,
	    'user_id'   => 2,
	    'svg_file1' => '/home/baiwei/poster_backgroup.png',
	    'svg_file2' => '/home/baiwei/poster_event.png',
	    ),
);

$request_client = new RequestClient();

// 如要要测试,可以使用tests/server.go提供的上传功能来作为测试服务器
$server_url = "http://127.0.0.1:18888/upload";

$options = array(
        RequestOptions::DEBUG  => 1,
        RequestOptions::MULTIPART => array(
            'id'        => 1,
            'user_id'   => 2,
            'file' => '/home/baiwei/poster_backgroup.png',
            ),
        );
$request_uniq = $request_client->addRequest($server_url, $options, RequestClient::MODE_ASYNC);
$ret = $request_client->getResponse($request_uniq);
// 对请求结果进行处理
// ...