PHP code example of verdient / saiya

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

    

verdient / saiya example snippets


use Verdient\Saiya\Saiya;

/**
 * 创建客户端
 * @param string $host 主机地址
 * @param string $port 端口
 * @param string $accessKey 授权标识
 * @param string $accessSecret 授权秘钥
 */
$saiya = new Saiya([
    'host' => '127.0.0.1',
    'port' => 80,
    'accessKey' => 'XXX',
    'accessSecret' => 'XXXX'
]);


$request = $saiya->request($path);

$request->setHeaders([$name => $value, ...]); //设置请求头部
$request->setQuery([$name => $value, ...]); //设置查询参数
$request->setBody([$name => $value, ...]); //设置消息体参数
$request->setProxy($address, $port=null); //设置代理
$request->setTimeout($timeout); //设置超时时间

$request->setContent($data, $serializer = null);

$response = $request->send();

$response->getIsOK(); //获取请求是否成功
$response->getData(); //获取请求地址
$response->getErrorCode(); //获取错误码
$response->getErrorMessage(); //获取错误信息
$response->getResponse(); //获取原始响应对象
$response->getResponse()->getRawResponse(); //获取响应原文
$response->getResponse()->getRawContent(); //获取消息体原文
$response->getResponse()->getRawHeaders(); //获取头部原文
$response->getResponse()->getBody(); //获取解析后的消息体参数
$response->getResponse()->getHeaders(); //获取解析后的头部
$response->getResponse()->getCookies(); //获取解析后的Cookie
$response->getResponse()->getStatusCode(); //获取状态码
$response->getResponse()->getContentType(); //获取消息体类型
$response->getResponse()->getCharset(); //获取字符集
$response->getResponse()->getStatusMessage(); //获取状态消息
$response->getResponse()->getHttpVersion(); //获取HTTP版本

use Verdient\http\BatchRequest;

/**
 * 传输组件配置
 * 内置了三种传输组件,分别是:
 *   cUrl, 基于cUrl的传输组件
 *   coroutine 基于Swoole的协程的传输组件
 *   stream 基于Streams的传输组件
 * 可自行新增或覆盖相应的传输组件
 */
$transports = [
    'cUrl' => 'Verdient\http\transport\CUrlTransport',
    'coroutine' => 'Verdient\http\transport\CoroutineTransport',
    'stream' => 'Verdient\http\transport\StreamTransport'
];

/**
 * 传输组件,默认为cUrl
 */
$transport = 'cUrl';

/**
 * 批大小,默认为100
 */
$batchSize = 100;

$batch = new BatchRequest([
    'batchSize' => $batchSize,
    'transports' => $transports,
    'transport' => $transport,
]);

/**
 * 请求对象的集合
 * 集合内的元素必须是Verdient\http\Request的实例
 */
$requests = [];


for($i = 0; $i < 100; $i++){
    $request = $saiya->request($path);
    $request->addQuery('id', $i);
    ...
    $requests[] = $request;
}

$batch->setRequests($requests);

/**
 * 返回内容为数组,keyValue对应关系与构造BatchRequest时传入的数组相同
 * 遍历返回的结果,结果与Request调用send方法后返回的内容一致,使用方法也相同
 */
$response = $batch->send();