Ejemplo n.º 1
0
<?php

require __DIR__ . '/../vendor/autoload.php';
try {
    // Instantiate the HTTP client
    $client = new Amp\Artax\Client();
    // Make an asynchronous HTTP request
    $promise = $client->request('http://httpbin.org/user-agent');
    // Client::request() is asynchronous! It doesn't return a response. Instead, it
    // returns a promise to resolve the response at some point in the future when
    // it's finished. Here we use the Amp concurrency framework to synchronously wait
    // for the eventual promise result.
    $response = \Amp\wait($promise);
    // Output the results
    printf("\nHTTP/%s %d %s\n", $response->getProtocol(), $response->getStatus(), $response->getReason());
} catch (Amp\Artax\ClientException $error) {
    // If something goes wrong the Promise::wait() call will throw the relevant
    // exception. The Client::request() method itself will never throw.
    echo $error;
}
Ejemplo n.º 2
0
<?php

require __DIR__ . '/../vendor/autoload.php';
try {
    // Instantiate the HTTP client
    $client = new Amp\Artax\Client();
    // Let's build up a custom Request object
    $request = (new Amp\Artax\Request())->setMethod('GET')->setUri('http://httpbin.org/user-agent')->setHeader('X-My-Header', 'some-value');
    // Make an asynchronous HTTP request
    $promise = $client->request($request);
    // Client::request() is asynchronous! It doesn't return a response. Instead, it
    // returns a promise to resolve the response at some point in the future when
    // it's finished. Here we use the Amp concurrency framework to synchronously wait
    // for the eventual promise result.
    $response = \Amp\wait($promise);
    // Output the results
    printf("\nHTTP/%s %d %s\n", $response->getProtocol(), $response->getStatus(), $response->getReason());
} catch (Amp\Artax\ClientException $e) {
    // If something goes wrong the Promise::wait() call will throw the relevant
    // exception. The Client::request() method itself will never throw.
    echo $e;
}
Ejemplo n.º 3
0
function prepareArtaxClient(Amp\Artax\Client $client)
{
    $client->setOption(\Amp\Artax\Client::OP_MS_CONNECT_TIMEOUT, 5000);
    $client->setOption(\Amp\Artax\Client::OP_MS_KEEP_ALIVE_TIMEOUT, 2000);
}
Ejemplo n.º 4
0
<?php

require __DIR__ . '/../vendor/autoload.php';
try {
    // Instantiate the HTTP client
    $client = new Amp\Artax\Client();
    // Enable verbose sends so we can see our raw request messages in the console
    // as they're sent to the server.
    $client->setOption(Amp\Artax\Client::OP_VERBOSITY, Amp\Artax\Client::VERBOSE_SEND);
    // This request will receive and store google's Set-Cookie headers.
    $promise = $client->request('http://www.google.com/');
    $response = \Amp\wait($promise);
    // And this request will send the cookie we received in the first request.
    // In your console you'll see that this second request contains a Cookie header.
    $promise = $client->request('http://www.google.com/');
    $response = \Amp\wait($promise);
} catch (Amp\Artax\ClientException $e) {
    // If something goes wrong the Promise::wait() call will throw the relevant
    // exception. The Client::request() method itself will never throw.
    echo $e;
}
Ejemplo n.º 5
0
 * Amp\Artax\Client::request() will only resolve as a failure if something goes seriously wrong with
 * the request/response cycle:
 *
 *     - infinite redirect loop
 *     - invalid/unparsable HTTP response message received from server
 *     - DNS resolution failure
 *     - premature loss of socket connection
 *     - malformed request URI
 *     - etc.
 *
 * All successful responses are modeled as an Amp\Artax\Response and this result is used to resolve
 * the promise result. Status codes are accessible via the standard Amp\Artax\Response::getStatus()
 * method. These should be consulted to determine the success or failure of completed responses.
 */
$badUri = "this isn't even a real URI!";
$client = new Amp\Artax\Client();
// Yielding a promise that fails will result in an exception
// being thrown back into your generator.
Amp\run(function () use($client, $badUri) {
    try {
        $response = (yield $client->request($badUri));
    } catch (Exception $e) {
        echo $e->getMessage(), "\n";
    }
});
// Synchronously waiting on a promise that fails will throw.
try {
    $response = Amp\wait($client->request($badUri));
} catch (Exception $e) {
    echo $e->getMessage(), "\n";
}