Standalone Drupal 8 script using Guzzle

Drupal 8 will have web services as part of core which will provide native support for all entity types including custom entity types. Drupal 8 will have support for different serialization formats like HAL+JSON, XML and JSON.

A couple good article references:

Additionally, Drupal 8 has adopted the Guzzle library to replace the drupal_http_request() function which provides a feature rich HTTP Client. I was looking for a simple standalone script to test using Guzzle to read a node from D8 using the RESTful endpoint and get a hal+json format resonse.

/* Script located in the docroot for your Drupal 8 site */
use Drupal\Core\DrupalKernel;
use Drupal\Core\Site\Settings;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once __DIR__ . '/core/vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
echo '<h1>RESTful request to get a json response for node 1</h1>';
$request = $client->createRequest('GET', 'http://localhost/d8test2/node/1');
// Set the Accept header for the D8 JSON representation
$request->setHeader('Accept', 'application/hal+json');
$response = $client->send($request);
$json = $response->json();
echo '<pre>' . var_dump($json) . '</pre><hr>';
// Now display just the node body value
echo '<h1>Node 1 body value</h1>';
print $json['body'][0]['value'];
echo '<h1>Retrieve the complete site</h1>';
$response = $client->get('http://localhost/d8test2/node/1');
$body = $response->getBody();
// By default if the response is large, Guzzle will save the body in a temp file
while (!$body->eof()) {
echo $body->read(1024);

Link to gist on github: