Class yii\httpclient\CurlTransport

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

CurlTransport sends HTTP messages using Client URL Library (cURL)

Note: this transport requires PHP 'curl' extension installed.

For this transport, you may setup request options as cURL Options

Public Methods

Hide inherited methods

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

Method Details

Hide inherited methods

batchSend() public method

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)
{
    $curlBatchResource = curl_multi_init();
    $token = '';
    $curlResources = [];
    $responseHeaders = [];
    foreach ($requests as $key => $request) {
        /* @var $request Request */
        $request->beforeSend();
        $curlOptions = $this->prepare($request);
        $curlResource = $this->initCurl($curlOptions);
        $token .= $request->client->createRequestLogToken($request->getMethod(), $curlOptions[CURLOPT_URL], $curlOptions[CURLOPT_HTTPHEADER], $request->getContent()) . "\n\n";
        $responseHeaders[$key] = [];
        $this->setHeaderOutput($curlResource, $responseHeaders[$key]);
        $curlResources[$key] = $curlResource;
        curl_multi_add_handle($curlBatchResource, $curlResource);
    }
    Yii::info($token, __METHOD__);
    Yii::beginProfile($token, __METHOD__);
    try {
        $isRunning = null;
        do {
            // See https://bugs.php.net/bug.php?id=61141
            if (curl_multi_select($curlBatchResource) === -1) {
                usleep(100);
            }
            do {
                $curlExecCode = curl_multi_exec($curlBatchResource, $isRunning);
            } while ($curlExecCode === CURLM_CALL_MULTI_PERFORM);
        } while ($isRunning > 0 && $curlExecCode === CURLM_OK);
    } catch (\Exception $e) {
        Yii::endProfile($token, __METHOD__);
        throw new Exception($e->getMessage(), $e->getCode(), $e);
    }
    Yii::endProfile($token, __METHOD__);
    $responseContents = [];
    foreach ($curlResources as $key => $curlResource) {
        $responseContents[$key] = curl_multi_getcontent($curlResource);
        curl_multi_remove_handle($curlBatchResource, $curlResource);
    }
    curl_multi_close($curlBatchResource);
    $responses = [];
    foreach ($requests as $key => $request) {
        $response = $request->client->createResponse($responseContents[$key], $responseHeaders[$key]);
        $request->afterSend($response);
        $responses[$key] = $response;
    }
    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();
    $curlOptions = $this->prepare($request);
    $curlResource = $this->initCurl($curlOptions);
    $responseHeaders = [];
    $this->setHeaderOutput($curlResource, $responseHeaders);
    $token = $request->client->createRequestLogToken($request->getMethod(), $curlOptions[CURLOPT_URL], $curlOptions[CURLOPT_HTTPHEADER], print_r($request->getContent(), true));
    Yii::info($token, __METHOD__);
    Yii::beginProfile($token, __METHOD__);
    $responseContent = curl_exec($curlResource);
    Yii::endProfile($token, __METHOD__);
    // check cURL error
    $errorNumber = curl_errno($curlResource);
    $errorMessage = curl_error($curlResource);
    curl_close($curlResource);
    if ($errorNumber > 0) {
        throw new Exception('Curl error: #' . $errorNumber . ' - ' . $errorMessage, $errorNumber);
    }
    $response = $request->client->createResponse($responseContent, $responseHeaders);
    $request->afterSend($response);
    return $response;
}