PHP code example of johnshopkins / http-exchange

1. Go to this page and download the library: Download johnshopkins/http-exchange library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

johnshopkins / http-exchange example snippets


$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle7($client);

$response = $http->get('http://httpbin.org/get');
$body = $response->getBody();
echo $body->url;

// prints: http://httpbin.org/get

$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle7($client);

$responses = $http->batch([
  ['get', 'http://httpbin.org/get'],
  ['post', 'http://httpbin.org/post']
]);

foreach ($responses as $response) {
  $body = $response->getBody();
  echo $body->url . "\n";
}

// prints:
// http://httpbin.org/get
// http://httpbin.org/post

$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle7($client);

try {
  $response = $http->get('http://httpbin.org/status/503');
  $body = $response->getBody();
} catch (\Exception $e) {
  echo $->getCode() . ': ' . $e->getMessage();
}

// prints:  503: Server error: `GET http://httpbin.org/status/503` resulted in a `503 SERVICE UNAVAILABLE` response

$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle7($client);

$responses = $http->batch([
  ['get', 'http://httpbin.org/get'],
  ['get', 'http://httpbin.org/status/503']
]);

foreach ($responses as $response) {
  if ($response->getStatusCode() === 200) {
    $body = $response->getBody();
    echo $body->url . "\n";
  } else {
    echo $body->url . "This request failed :(\n";
  }
}

// prints:
// http://httpbin.org/get
// This request failed :(

$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle7($client);

$responses = $http->batch([
  ['get', 'http://httpbin.org/get'],
  ['get', 'http://httpbin.org/status/503']
]);

foreach ($responses as $response) {
  if ($response instanceof HttpExchange\Response) {
    $body = $response->getBody();
    echo $body->url . "\n";
  } else {
    echo $body->url . "This request failed :(\n";
  }
}

// prints:
// http://httpbin.org/get
// This request failed :(

$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle6($client);

$client = new GuzzleHttp\Client();
$http = new HttpExchange\Adapters\Guzzle7($client);

  $requests = [[$method, $url, $request_options], ...];