Class yii\httpclient\StreamTransport

Inheritanceyii\httpclient\StreamTransport » yii\httpclient\Transport » yii\base\Component
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-httpclient/blob/master/src/StreamTransport.php

StreamTransport sends HTTP messages using Streams

For this transport, you may setup request options using Context Options

Public Methods

Hide inherited methods

Method Description Defined By
batchSend() Performs multiple HTTP requests. yii\httpclient\Transport
send() Performs given request. yii\httpclient\StreamTransport

Method Details

Hide inherited methods

batchSend() public method

Defined in: yii\httpclient\Transport::batchSend()

Performs multiple HTTP requests.

Particular transport may benefit from this method, allowing sending requests in parallel. This method accepts an array of the yii\httpclient\Request objects and returns an array of the yii\httpclient\Response objects. Keys of the response array correspond the ones from request array.

public yii\httpclient\Response[] batchSend ( array $requests )
$requests yii\httpclient\Request[]

Requests to perform.

return yii\httpclient\Response[]

Responses list.

throws yii\httpclient\Exception

                public function batchSend(array $requests)
{
    $responses = [];
    foreach ($requests as $key => $request) {
        $responses[$key] = $this->send($request);
    }
    return $responses;
}

            
send() public method

Performs given request.

public yii\httpclient\Response send ( $request )
$request yii\httpclient\Request

Request to be sent.

return yii\httpclient\Response

Response instance.

throws yii\httpclient\Exception

on failure.

                public function send($request)
{
    $request->beforeSend();
    $request->prepare();
    $url = $request->getFullUrl();
    $method = strtoupper($request->getMethod());
    $contextOptions = [
        'http' => [
            'method' => $method,
            'ignore_errors' => true,
        ],
        'ssl' => [
            'verify_peer' => false,
        ],
    ];
    $content = $request->getContent();
    if ($content !== null) {
        $contextOptions['http']['content'] = $content;
    }
    $headers = $request->composeHeaderLines();
    $contextOptions['http']['header'] = $headers;
    $contextOptions = ArrayHelper::merge($contextOptions, $this->composeContextOptions($request->getOptions()));
    $token = $request->client->createRequestLogToken($method, $url, $headers, $content);
    Yii::info($token, __METHOD__);
    Yii::beginProfile($token, __METHOD__);
    try {
        $context = stream_context_create($contextOptions);
        $stream = fopen($url, 'rb', false, $context);
        $responseContent = stream_get_contents($stream);
        // see http://php.net/manual/en/reserved.variables.httpresponseheader.php
        $responseHeaders = (array)$http_response_header;
        fclose($stream);
    } catch (\Exception $e) {
        Yii::endProfile($token, __METHOD__);
        throw new Exception($e->getMessage(), $e->getCode(), $e);
    }
    Yii::endProfile($token, __METHOD__);
    $response = $request->client->createResponse($responseContent, $responseHeaders);
    $request->afterSend($response);
    return $response;
}