Send the request(s) via curl

@param  array|\Yukikey\RequestCollection $requests Set of \Yubikey\Request objects
@return \Yubikey\ResponseCollection instance
966805
public function send($requests)
    {
        if (get_class($requests) !== 'Yubikey\\RequestCollection') {
            $requests = new \Yubikey\RequestCollection($requests);
        }

        $responses = $this->request($requests);
        return $responses;
    }
546368
public function send(\Psr\Http\Message\RequestInterface $request)
    {
        if ($this->credentials instanceof Container) {
            if (strpos($request->getUri()->getPath(), '/v1/calls') === 0) {
                $request = $request->withHeader('Authorization', 'Bearer '.$this->credentials->get(Keypair::class)->generateJwt());
            } else {
                $request = self::authRequest($request, $this->credentials->get(Basic::class));
            }
        } elseif ($this->credentials instanceof Keypair) {
            $request = $request->withHeader('Authorization', 'Bearer '.$this->credentials->get(Keypair::class)->generateJwt());
        } elseif ($this->credentials instanceof SharedSecret) {
            $request = self::signRequest($request, $this->credentials);
        } elseif ($this->credentials instanceof Basic) {
            $request = self::authRequest($request, $this->credentials);
        }

        //todo: add oauth support

        //allow any part of the URI to be replaced with a simple search
        if (isset($this->options['url'])) {
            foreach ($this->options['url'] as $search => $replace) {
                $uri = (string) $request->getUri();

                $new = str_replace($search, $replace, $uri);
                if ($uri !== $new) {
                    $request = $request->withUri(new Uri($new));
                }
            }
        }

        $response = $this->client->sendRequest($request);

        return $response;
    }
186491
public function deleteCollectionAction(Request $request)
    {
        if (null === $uids = $request->get('uids', null)) {
            throw new BadRequestHttpException('uid is missing.');
        }

        $result = [];
        $statusCode = 204;
        $pages = $this->getPageRepository()->findBy(['_uid' => $uids]);
        foreach ($pages as $page) {
            try {
                if ($page->isDeleted()) {
                    $this->hardDelete($page);
                } else {
                    $this->softDelete($page);
                }

                $result[] = [
                    'uid'        => $page->getUid(),
                    'statusCode' => 204,
                    'message'    => 'OK',
                ];
            } catch (AccessDeniedException $e) {
                $result[] = [
                    'uid'        => $page->getUid(),
                    'statusCode' => 401,
                    'message'    => $e->getMessage(),
                ];
                $statusCode = ($statusCode < 401) ? 401 : $statusCode;
            } catch (\Exception $e) {
                if ($e instanceof BadRequestHttpException || $e instanceof InsufficientAuthenticationException) {
                    $result[] = [
                        'uid'        => $page->getUid(),
                        'statusCode' => 403,
                        'message'    => $e->getMessage(),
                    ];
                    $statusCode = ($statusCode < 403) ? 403 : $statusCode;
                } else {
                    $result[] = [
                        'uid'        => $page->getUid(),
                        'statusCode' => 500,
                        'message'    => $e->getMessage(),
                    ];
                    $statusCode = ($statusCode < 500) ? 500 : $statusCode;
                }
            }
        }

        $this->getEntityManager()->flush();

        return $this->createJsonResponse($result, $statusCode);
    }
178476
public function sendRequest(RequestInterface $request)
    {
        if (false === is_resource($this->resource)) {
            $this->resource = curl_init();
        }

        $response = $this->channelBuilder
            ->channel($this->resource)
            ->options(array_replace($this->options, $this->requestOptions))
            ->request($request)
            ->response($this->responseFactory->createResponse())
            ->getChannel()
            ->send();

        $response->getBody()->rewind();
        $this->requestOptions = [];

        return $response;
    }
236277
private function send(Request $request)
    {
        $options = [
            'base_uri' => self::BASE_URI,
            'http_errors' => false,
        ];

        return $this->client->send($request, $options);
    }
612920
public function sendRequest(Http\Request $request)
    {
        if (!extension_loaded('curl')) {
            throw new Http\Exception('CurlEngine requires the PHP CURL extension to be installed and loaded.', 1346319808);
        }

        $requestUri = $request->getUri();
        $curlHandle = curl_init((string)$requestUri);

        curl_setopt_array($curlHandle, $this->options);

        // Send an empty Expect header in order to avoid chunked data transfer (which we can't handle yet).
        // If we don't set this, cURL will set "Expect: 100-continue" for requests larger than 1024 bytes.
        curl_setopt($curlHandle, CURLOPT_HTTPHEADER, ['Expect:']);

        // If the content is a stream resource, use cURL's INFILE feature to stream it
        $content = $request->getContent();
        if (is_resource($content)) {
            curl_setopt_array($curlHandle,
                [
                    CURLOPT_INFILE => $content,
                    CURLOPT_INFILESIZE => $request->getHeader('Content-Length'),
                ]
            );
        }

        switch ($request->getMethod()) {
            case 'GET':
                if ($request->getContent()) {
                    // workaround because else the request would implicitly fall into POST:
                    curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'GET');
                    if (!is_resource($content)) {
                        curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $content);
                    }
                }
            break;
            case 'POST':
                curl_setopt($curlHandle, CURLOPT_POST, true);
                if (!is_resource($content)) {
                    $body = $content !== '' ? $content : http_build_query($request->getArguments());
                    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $body);
                }
            break;
            case 'PUT':
                curl_setopt($curlHandle, CURLOPT_PUT, true);
                if (!is_resource($content) && $content !== '') {
                    $inFileHandler = fopen('php://temp', 'r+');
                    fwrite($inFileHandler, $request->getContent());
                    rewind($inFileHandler);
                    curl_setopt_array($curlHandle, [
                        CURLOPT_INFILE => $inFileHandler,
                        CURLOPT_INFILESIZE => strlen($request->getContent()),
                    ]);
                }
            break;
            case 'HEAD':
                if (!is_resource($content)) {
                    $body = $content !== '' ? $content : http_build_query($request->getArguments());
                    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $body);
                }
                curl_setopt($curlHandle, CURLOPT_NOBODY, true);
            break;
            default:
                if (!is_resource($content)) {
                    $body = $content !== '' ? $content : http_build_query($request->getArguments());
                    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $body);
                }
                curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, $request->getMethod());
        }

        $preparedHeaders = [];
        foreach ($request->getHeaders()->getAll() as $fieldName => $values) {
            foreach ($values as $value) {
                $preparedHeaders[] = $fieldName . ': ' . $value;
            }
        }
        curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $preparedHeaders);

        // curl_setopt($curlHandle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP && CURLPROTO_HTTPS);
        // CURLOPT_UPLOAD

        if ($requestUri->getPort() !== null) {
            curl_setopt($curlHandle, CURLOPT_PORT, $requestUri->getPort());
        }

        if (count($request->getCookies()) > 0) {
            $cookies = [];
            foreach ($request->getCookies() as $cookie) {
                $cookies[] = $cookie->getName() . '=' . $cookie->getValue();
            }
            curl_setopt($curlHandle, CURLOPT_COOKIE, implode('; ', $cookies));
        }

        $curlResult = curl_exec($curlHandle);
        if ($curlResult === false) {
            throw new CurlEngineException(sprintf('cURL reported error code %s with message "%s". Last requested URL was "%s" (%s).', curl_errno($curlHandle), curl_error($curlHandle), curl_getinfo($curlHandle, CURLINFO_EFFECTIVE_URL), $request->getMethod()), 1338906040);
        } elseif (strlen($curlResult) === 0) {
            return false;
        }
        curl_close($curlHandle);

        $response = Http\Helper\ResponseInformationHelper::createFromRaw($curlResult);
        try {
            while (substr($response->getContent(), 0, 5) === 'HTTP/' || $response->getStatusCode() === 100) {
                $response = Http\Response::createFromRaw($response->getContent(), $response);
            }
        } catch (\InvalidArgumentException $e) {
        }

        return $response;
    }
905262
public function send(Request $request)
    {
        $response   = new PutResponse();
        $rpbPutReq  = $this->createRpbMessage($request);
        $rpbPutResp = $this->client->send($rpbPutReq, RiakMessageCodes::PUT_REQ, RiakMessageCodes::PUT_RESP);

        if ( ! $rpbPutResp instanceof RpbPutResp) {
            return $response;
        }

        if ( ! $rpbPutResp->hasContent()) {
            return $response;
        }

        if ($rpbPutResp->hasKey()) {
            $response->key = $rpbPutResp->key;
        }

        $response->vClock      = $rpbPutResp->getVclock()->get();
        $response->contentList = $this->createContentList($rpbPutResp->getContentList());

        return $response;
    }
1157802
public function sendRequest(Request $request)
    {
        foreach ($this->automaticRequestHeaders->getAll() as $name => $values) {
            $request->setHeader($name, $values);
        }

        $this->lastRequest = $request;
        $this->lastResponse = $this->requestEngine->sendRequest($request);
        return $this->lastResponse;
    }
874128
protected function doRequest(Request $request)
    {

        $this->client->send($request, $response = new Response());

        return $response;
    }
349853
public function send(Request $request)
    {
        $response   = new GetIndexResponse();
        $rpbGetReq  = $this->createRpbMessage($request);
        $rpbGetResp = $this->client->send($rpbGetReq, RiakMessageCodes::YOKOZUNA_INDEX_GET_REQ, RiakMessageCodes::YOKOZUNA_INDEX_GET_RESP);

        if ( ! $rpbGetResp instanceof RpbYokozunaIndexGetResp) {
            return $response;
        }

        if ( ! $rpbGetResp->hasIndex()) {
            return $response;
        }

        $rpbIndex = is_array($rpbGetResp->index)
            ? reset($rpbGetResp->index)
            : $rpbGetResp->index;

        $response->nVal   = $rpbIndex->n_val;
        $response->name   = $rpbIndex->name;
        $response->schema = $rpbIndex->schema;

        return $response;
    }
913421
public function send(RequestInterface $request)
    {
        $apiResponse = null;

        try {
            $apiResponse = $this->loadResponse($request);

            if ($apiResponse->ok()) {
                return $apiResponse;
            } else {
                throw new Exception('Response has unsuccessful status');
            }
        } catch (Exception $e) {
            // The following means that request failed completely
            if (empty($apiResponse)) {
                $apiResponse = new ApiResponse($request);
            }

            throw new ApiException($apiResponse, $e);
        }
    }
1369548
public function sendRequest(Request $request)
    {

        $result = $this->responseFactory->create();
        $client = $this->httpClientFactory->create();

        $apiData = $request->getApiData();
        $apiUrl = $request->getApiUrl();
        $apiKey = $request->getApiKey();
        $requestMethod = $request->getRequestMethod();

        $requestData = $requestMethod !== "GET" ? json_encode($apiData, JSON_UNESCAPED_SLASHES) : null;

        $client->setUri($apiUrl);

        $client->setConfig(['maxredirects' => 0, 'timeout' => 30]);

        $headers =  [
            'User-Agent'            => 'BoltPay/Magento-'.$this->configHelper->getStoreVersion() . '/' . $this->configHelper->getModuleVersion(),
            'X-Bolt-Plugin-Version' => $this->configHelper->getModuleVersion(),
            'Content-Type'          => 'application/json',
            'Content-Length'        => $requestData ? strlen($requestData) : null,
            'X-Api-Key'             => $apiKey,
            'X-Nonce'               => rand(100000000000, 999999999999)
        ] + (array)$request->getHeaders();

        $request->setHeaders($headers);

        $this->bugsnag->registerCallback(function ($report) use ($request) {
            $report->setMetaData([
                'BOLT API REQUEST' => $request->getData()
            ]);
        });

        $client->setHeaders($headers);

        $responseBody = null;

        try {
            $response     = $client->setRawData($requestData, 'application/json')->request($requestMethod);
            $responseBody = $response->getBody();

            $this->bugsnag->registerCallback(function ($report) use ($response) {
                $report->setMetaData([
                    'BOLT API RESPONSE' => [
                        'headers' => $response->getHeaders(),
                        'body'    => $response->getBody(),
                    ]
                ]);
            });

            $this->bugsnag->registerCallback(function ($report) use ($response) {
                $headers = $response->getHeaders();
                $report->setMetaData([
                    'META DATA' => [
                        'bolt_trace_id' => @$headers[ConfigHelper::BOLT_TRACE_ID_HEADER],
                    ]
                ]);
            });
        } catch (\Exception $e) {
            throw new LocalizedException($this->wrapGatewayError($e->getMessage()));
        }

        if ($request->getStatusOnly() && $response) {
            return $response->getStatus();
        }

        if ($responseBody) {
            $resultFromJSON = json_decode($responseBody);
            $jsonError  = $this->handleJsonParseError();
            if ($jsonError != null) {
                $message = __("JSON Parse Error: " . $jsonError);
                throw new LocalizedException($message);
            }

            if ($this->isResponseError($resultFromJSON)) {
                $message = isset($resultFromJSON->errors[0]->message) ? __($resultFromJSON->errors[0]->message) :  __("Bolt API Error Response");
                throw new LocalizedException($message);
            }

            $result->setResponse($resultFromJSON);
        } else {
            throw new LocalizedException(
                __('Something went wrong in the payment gateway.')
            );
        }
        return $result;
    }
73259
protected function sendMultiple(array $requests)
    {
        $curlMulti = $this->getCurlMulti();
        foreach ($requests as $request) {
            $curlMulti->add($request);
        }
        $curlMulti->send();

        /** @var $request RequestInterface */
        $result = array();
        foreach ($requests as $request) {
            $result[] = $request->getResponse();
        }

        return $result;
    }
794242
public function send(Request $request)
    {
        $response  = new ListKeysResponse();
        $rpbGetReq = $this->createRpbMessage($request);
        $socket    = $this->client->emit($rpbGetReq, RiakMessageCodes::LIST_KEYS_REQ);
        $iterator  = new ProtoStreamIterator($this->client, $socket, RiakMessageCodes::LIST_KEYS_RESP);

        $response->iterator = new ProtoListKeysResponseIterator($iterator);

        return $response;
    }
1303720
public function send(RequestInterface $request)
    {
        /** @var CurlTransport $transport */
        $transport = $this->getTransport();

        $this->tasks[] = [
            'handle' => $handle = $transport->createHandle($request),
            'promise' => $promise = new Promise()
        ];

        curl_multi_add_handle($this->getMainHandle(), $handle);

        return $promise;
    }
1243434
public function sendBatchRequest(BatchRequest $request)
    {
        $response = $this->sendRequest($request);

        return new BatchResponse($request, $response);
    }
1008938
public function send(Request $request)
    {
        $httpRequest  = $this->createHttpRequest($request);
        $httpResponse = $this->client->send($httpRequest);
        $code         = $httpResponse->getStatusCode();
        $response     = new PutResponse();

        if ( ! isset($this->validResponseCodes[$code])) {
            throw RiakTransportException::unexpectedStatusCode($code);
        }

        $contentList = $this->getRiakContentList($httpResponse);
        $vClock      = $httpResponse->getHeader('X-Riak-Vclock');
        $key         = null;

        if ($httpResponse->hasHeader('Location')) {
            $location = $httpResponse->getHeader('Location');
            $key      = substr($location, strrpos($location, '/') + 1);
        }

        $response->key         = $key;
        $response->vClock      = $vClock;
        $response->contentList = $contentList;

        return $response;
    }