虚拟社区

程序开发 => PHP 开发 => WEB后端开发 => PHP框架-Yii => 主题发帖人为: jvip_chen 于 2022-5月-22 05:36 下午

标题: Request 参数
作者: jvip_chen2022-5月-22 05:36 下午
使用 yii\httpclient\Request::$options 来调整特定的请求。 支持以下参数:

例如:

use yii\httpclient\Client;

$client = new Client();

$response = $client->createRequest()
    ->setMethod('POST')
    ->setUrl('http://domain.com/api/1.0/users')
    ->setData(['name' => 'John Doe', 'email' => 'johndoe@domain.com'])
    ->setOptions([
        'proxy' => 'tcp://proxy.example.com:5100', // use a Proxy
        'timeout' => 5, // set timeout to 5 seconds for the case server is not responding
    ])
    ->send();
提示:可以通过 yii\httpclient\Client::$requestConfig 对默认请求进行设置。 可以使用 yii\httpclient\Request::addOptions() 在添加其他设置的同时保留原始的其他参数。

在使用 yii\httpclient\CurlTransport 时,可配置特殊的请求参数。 例如:指定 cUrl 请求的连接和接收数据的超时时间:

use yii\httpclient\Client;

$client = new Client([
    'transport' => 'yii\httpclient\CurlTransport' // only cURL supports the options we need
]);

$response = $client->createRequest()
    ->setMethod('POST')
    ->setUrl('http://domain.com/api/1.0/users')
    ->setData(['name' => 'John Doe', 'email' => 'johndoe@domain.com'])
    ->setOptions([
        CURLOPT_CONNECTTIMEOUT => 5, // connection timeout
        CURLOPT_TIMEOUT => 10, // data receiving timeout
    ])
    ->send();
有关特定参数支持的详细信息,请参阅特定的传输类文档。