Пример #1
0
<?php

use Icicle\Http\Client\Client;
use Icicle\Http\Message\RequestInterface;
use Icicle\Http\Message\Response;
use Icicle\Http\Message\ResponseInterface;
use Icicle\Socket\SocketInterface;
use Icicle\Stream\MemorySink;
$collector->addRoute("GET", "/", function (RequestInterface $request, SocketInterface $socket) {
    $stream = new MemorySink();
    (yield $stream->end("hello world"));
    (yield new Response(200, ["content-type" => "text/html", "content-length" => $stream->getLength()], $stream));
});
$collector->addRoute("GET", "/test", function (RequestInterface $request, SocketInterface $socket) {
    $client = new Client();
    /** @var ResponseInterface $response */
    $response = (yield $client->request("GET", "http://jsonplaceholder.typicode.com/posts/1"));
    $stream1 = $response->getBody();
    $stream2 = new MemorySink();
    while ($stream1->isReadable()) {
        $data = (yield $stream1->read());
        (yield $stream2->write($data));
    }
    (yield $stream2->end());
    (yield new Response(200, ["content-type" => "text/html", "content-length" => $stream2->getLength()], $stream2));
});
Пример #2
0
 /**
  * @param string $pathName
  * @param array $params
  *
  * @yield Generator
  */
 protected function post(string $pathName, array $params = [], $headers = [], ReadableStream $body = null) : \Generator
 {
     $url = $this->buildUrl($pathName, $params);
     return $this->httpClient->request('POST', $url, $headers, $body, ['timeout' => 60]);
 }
Пример #3
0
 /**
  * @param string $method
  * @param        $uri
  * @param array  $headers
  * @param null   $body
  * @param array  $options
  *
  * @return \Generator
  */
 protected function request(string $method, $uri, array $headers = [], $body = null, array $options = []) : \Generator
 {
     return $this->httpClient->request($method, $uri, $headers, $body, $options);
 }
Пример #4
0
require "vendor/autoload.php";
use AsyncPHP\Icicle\Cache\Driver\MemoryDriver;
use Icicle\Http\Client\Client;
use Icicle\Http\Message\RequestInterface;
use Icicle\Http\Message\Response;
use Icicle\Http\Server\Server;
use Icicle\Loop;
use Icicle\Socket\SocketInterface;
use Icicle\Stream\MemorySink;
$cache = new MemoryDriver();
$server = new Server(function (RequestInterface $request, SocketInterface $socket) use($cache) {
    try {
        $cached = (yield $cache->get("foo"));
        if (!$cached) {
            $cached = (yield $cache->set("foo", function () {
                $client = new Client();
                /** @var ResponseInterface $response */
                $response = (yield $client->request("GET", "https://icicle.io/"));
                $data = "";
                $stream = $response->getBody();
                while ($stream->isReadable()) {
                    $data .= (yield $stream->read());
                }
                (yield $data);
            }));
        }
        $stream = new MemorySink();
        (yield $stream->end($cached));
        $response = new Response(200, ["content-type" => "text/html", "content-length" => $stream->getLength()], $stream);
        (yield $response);
        $stream = null;