Icicle\Http\Client\Client::send PHP Method

send() public method

public send ( Request $request, array $options = [] ) : Generator
$request Request
$options array
return Generator
    public function send(Request $request, array $options = []) : \Generator
    {
        $timeout = isset($options['timeout']) ? (double) $options['timeout'] : self::DEFAULT_TIMEOUT;
        $cryptoMethod = isset($options['crypto_method']) ? (int) $options['crypto_method'] : self::DEFAULT_CRYPTO_METHOD;
        $request = $request->withHeader('Connection', 'close');
        $count = 0;
        try {
            do {
                $uri = $request->getUri();
                $host = $uri->getHost();
                $port = $uri->getPort();
                if ('' === $host || 0 === $port) {
                    throw new InvalidArgumentError('Request URI must have a host and port.');
                }
                /** @var \Icicle\Socket\Socket $socket */
                $socket = (yield from Dns\connect($uri->getHost(), $uri->getPort(), $options));
                if ($uri->getScheme() === 'https') {
                    yield from $socket->enableCrypto($cryptoMethod, $timeout);
                }
                /** @var \Icicle\Http\Message\Response $response */
                $response = (yield from $this->requester->send($socket, $request, $options));
                if ($this->follow) {
                    switch ($response->getStatusCode()) {
                        case Response::SEE_OTHER:
                            $request = $request->withMethod($request->getMethod() === 'HEAD' ? 'HEAD' : 'GET');
                            // No break.
                        // No break.
                        case Response::MOVED_PERMANENTLY:
                        case Response::FOUND:
                        case Response::TEMPORARY_REDIRECT:
                        case Response::PERMANENT_REDIRECT:
                            $socket->close();
                            // Close original connection.
                            if (++$count > $this->maxRedirects) {
                                throw new RedirectException(sprintf('Too many redirects encountered (max redirects: %d).', $this->maxRedirects));
                            }
                            if (!$response->hasHeader('Location')) {
                                throw new RedirectException('No Location header found in redirect response.');
                            }
                            $request = $request->withUri(new BasicUri($response->getHeader('Location')));
                            $response = null;
                            // Let's go around again!
                    }
                }
            } while (null === $response);
        } finally {
            $request->getBody()->close();
        }
        return $response;
    }

Usage Example

Beispiel #1
0
 /**
  * Send image to a user
  *
  * @var int         $chatId
  * @var resource    $photo
  * @var string|null $caption
  * @var int|null    $replyToMessageId
  * @var mixed       $replyMarkup
  *
  * @return \Generator
  */
 public function sendPhoto(int $chatId, $photo, string $caption = null, int $replyToMessageId = null, $replyMarkup = null) : \Generator
 {
     $params = ['chat_id' => $chatId, 'caption' => $caption, 'replyToMessageId' => $replyToMessageId];
     $imageContentPipe = new ReadablePipe($photo);
     $boundary = uniqid();
     $mem = new MemoryStream();
     $contentLength = 0;
     $contentLength += (yield $mem->write("--{$boundary}\r\nContent-Disposition: form-data; name=\"photo\"; filename=\"example1.jpg\"\r\n\r\n"));
     $contentLength += (yield pipe($imageContentPipe, $mem, false));
     $contentLength += (yield $mem->end("\r\n--{$boundary}--"));
     $url = $this->buildUrl('/sendPhoto', $params);
     $headers = ['Content-type' => "multipart/form-data, boundary={$boundary}", 'Content-Length' => $contentLength];
     $request = new Request('POST', $url, $headers, $mem);
     $response = (yield $this->httpClient->send($request, []));
     $body = (yield $this->getResponseBody($response));
     $body = json_decode($body, true);
     (yield new Entity\Message($body['result']));
 }