Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 54 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ $client = new React\Http\Browser();

$client->get('http://www.google.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -171,8 +173,8 @@ $browser->get($url)->then(
function (Psr\Http\Message\ResponseInterface $response) {
var_dump('Response received', $response);
},
function (Exception $error) {
var_dump('There was an error', $error->getMessage());
function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}
);
```
Expand Down Expand Up @@ -232,6 +234,8 @@ $browser = $browser->withTimeout(10.0);
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
// response received within 10 seconds maximum
var_dump($response->getHeaders());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -319,6 +323,8 @@ The promise will be fulfilled with the last response from the chain of redirects
$browser->get($url, $headers)->then(function (Psr\Http\Message\ResponseInterface $response) {
// the final response will end up here
var_dump($response->getHeaders());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -347,6 +353,8 @@ $browser = $browser->withFollowRedirects(false);
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
// any redirects will now end up here
var_dump($response->getHeaders());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -410,6 +418,8 @@ from your side.
foreach ($urls as $url) {
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
}
```
Expand All @@ -429,6 +439,8 @@ $q = new Clue\React\Mq\Queue(10, null, function ($url) use ($browser) {
foreach ($urls as $url) {
$q($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
}
```
Expand Down Expand Up @@ -481,13 +493,15 @@ $browser->requestStreaming('GET', $url)->then(function (Psr\Http\Message\Respons
echo $chunk;
});

$body->on('error', function (Exception $error) {
echo 'Error: ' . $error->getMessage() . PHP_EOL;
$body->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$body->on('close', function () {
echo '[DONE]' . PHP_EOL;
});
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -549,6 +563,9 @@ $stream = download($browser, $url);
$stream->on('data', function ($data) {
echo $data;
});
$stream->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

See also the [`requestStreaming()`](#requeststreaming) method for more details.
Expand All @@ -565,6 +582,8 @@ to the [request methods](#request-methods) like this:
```php
$browser->post($url, array(), $stream)->then(function (Psr\Http\Message\ResponseInterface $response) {
echo 'Successfully sent.';
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -683,6 +702,8 @@ $browser = new React\Http\Browser($connector);

$client->get('http://localhost/info')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -1185,13 +1206,13 @@ $http = new React\Http\HttpServer(
});

// an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
$body->on('error', function (\Exception $exception) use ($resolve, &$bytes) {
$body->on('error', function (Exception $e) use ($resolve, &$bytes) {
$resolve(new React\Http\Message\Response(
400,
array(
'Content-Type' => 'text/plain'
),
"Encountered error after $bytes bytes: {$exception->getMessage()}\n"
"Encountered error after $bytes bytes: {$e->getMessage()}\n"
));
});
});
Expand Down Expand Up @@ -1914,6 +1935,8 @@ send an HTTP GET request.
```php
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump((string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand All @@ -1933,6 +1956,8 @@ $browser->post(
json_encode($data)
)->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump(json_decode((string)$response->getBody()));
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -1978,6 +2003,8 @@ send an HTTP HEAD request.
```php
$browser->head($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand All @@ -1995,6 +2022,8 @@ $browser->patch(
json_encode($data)
)->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump(json_decode((string)$response->getBody()));
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -2027,6 +2056,8 @@ $browser->put(
$xml->asXML()
)->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump((string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -2055,6 +2086,8 @@ send an HTTP DELETE request.
```php
$browser->delete($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump((string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand All @@ -2073,6 +2106,8 @@ can use this method:
```php
$browser->request('OPTIONS', $url)->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump((string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -2124,13 +2159,15 @@ $browser->requestStreaming('GET', $url)->then(function (Psr\Http\Message\Respons
echo $chunk;
});

$body->on('error', function (Exception $error) {
echo 'Error: ' . $error->getMessage() . PHP_EOL;
$body->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$body->on('close', function () {
echo '[DONE]' . PHP_EOL;
});
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -2209,6 +2246,8 @@ $browser = $browser->withFollowRedirects(0);
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
// only non-redirected responses will now end up here
var_dump($response->getHeaders());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand All @@ -2222,6 +2261,8 @@ $browser = $browser->withFollowRedirects(false);
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
// any redirects will now end up here
var_dump($response->getHeaderLine('Location'));
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -2253,6 +2294,8 @@ $browser = $browser->withRejectErrorResponse(false);
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
// any HTTP response will now end up here
var_dump($response->getStatusCode(), $response->getReasonPhrase());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand All @@ -2272,7 +2315,7 @@ $browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response
$response = $e->getResponse();
var_dump($response->getStatusCode(), $response->getReasonPhrase());
} else {
var_dump($e->getMessage());
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}
});
```
Expand Down Expand Up @@ -2362,6 +2405,8 @@ $browser = $browser->withResponseBuffer(1024 * 1024);
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
// response body will not exceed 1 MiB
var_dump($response->getHeaders(), (string) $response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down
2 changes: 2 additions & 0 deletions examples/01-client-get-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@

$client->get('http://google.com/')->then(function (ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
6 changes: 6 additions & 0 deletions examples/02-client-concurrent-requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@

$client->head('http://www.github.com/clue/http-react')->then(function (ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$client->get('http://google.com/')->then(function (ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$client->get('http://www.lueck.tv/psocksd')->then(function (ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
8 changes: 8 additions & 0 deletions examples/03-client-request-any.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@

var_dump($response->getHeaders());
echo PHP_EOL . $response->getBody();
}, function ($e) {
// Promise v1 and v2 reject with an array of Exceptions here, Promise v3 will use an Exception object instead
if (is_array($e)) {
$e = end($e);
}
assert($e instanceof Exception);

echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
4 changes: 3 additions & 1 deletion examples/04-client-post-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@
json_encode($data)
)->then(function (ResponseInterface $response) {
echo (string)$response->getBody();
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
4 changes: 3 additions & 1 deletion examples/05-client-put-xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
$xml->asXML()
)->then(function (ResponseInterface $response) {
echo (string)$response->getBody();
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
4 changes: 3 additions & 1 deletion examples/11-client-http-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@
// demo fetching HTTP headers (or bail out otherwise)
$browser->get('https://www.google.com/')->then(function (ResponseInterface $response) {
echo RingCentral\Psr7\str($response);
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
4 changes: 3 additions & 1 deletion examples/12-client-socks-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@
// demo fetching HTTP headers (or bail out otherwise)
$browser->get('https://www.google.com/')->then(function (ResponseInterface $response) {
echo RingCentral\Psr7\str($response);
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
4 changes: 3 additions & 1 deletion examples/13-client-ssh-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
// demo fetching HTTP headers (or bail out otherwise)
$browser->get('https://www.google.com/')->then(function (ResponseInterface $response) {
echo RingCentral\Psr7\str($response);
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
4 changes: 3 additions & 1 deletion examples/14-client-unix-domain-sockets.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
// demo fetching HTTP headers (or bail out otherwise)
$browser->get('http://localhost/info')->then(function (ResponseInterface $response) {
echo Psr7\str($response);
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
4 changes: 3 additions & 1 deletion examples/21-client-request-streaming-to-stdout.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@
$body = $response->getBody();
assert($body instanceof ReadableStreamInterface);
$body->pipe($out);
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
4 changes: 3 additions & 1 deletion examples/22-client-stream-upload-from-stdin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@

$client->post($url, array(), $in)->then(function (ResponseInterface $response) {
echo 'Received' . PHP_EOL . Psr7\str($response);
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
4 changes: 3 additions & 1 deletion examples/61-server-hello-world-https.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
));
$http->listen($socket);

$socket->on('error', 'printf');
$socket->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

echo 'Listening on ' . str_replace('tls:', 'https:', $socket->getAddress()) . PHP_EOL;
8 changes: 5 additions & 3 deletions examples/63-server-streaming-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ function (Psr\Http\Message\ServerRequestInterface $request) {
});

// an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
$body->on('error', function (\Exception $exception) use ($resolve, &$bytes) {
$body->on('error', function (Exception $e) use ($resolve, &$bytes) {
$resolve(new React\Http\Message\Response(
400,
array(
'Content-Type' => 'text/plain'
),
"Encountered error after $bytes bytes: {$exception->getMessage()}\n"
"Encountered error after $bytes bytes: {$e->getMessage()}\n"
));
});
});
}
);

$http->on('error', 'printf');
$http->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0');
$http->listen($socket);
Expand Down
Loading