Skip to content

Instantly share code, notes, and snippets.

@karens
Created October 21, 2015 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karens/35c4a372ba79fc2997b9 to your computer and use it in GitHub Desktop.
Save karens/35c4a372ba79fc2997b9 to your computer and use it in GitHub Desktop.
drupal httpclient example
$headers = [
'Accept' => 'application/json',
'Cache-Control' => 'no-cache',
'Authentication' => $auth_key,
];
try {
$client = \Drupal::httpClient();
$response = $client->get($url, array(
'headers' => $headers,
//'debug' => true,
));
if (empty($response)) {
//return FALSE;
}
}
catch (RequestException $e) {
//return FALSE;
//dpm($e);
}
$code = $response->getStatusCode();
$output = json_decode($response->getBody());
$output = $output->catalog;
$items = array();
foreach ($output as $item) {
switch($item->type) {
case 'Series':
$items[] = $item->_id . ': ' . $item->name;
break;
case 'Episode (Animated)':
break;
case 'Element (Animated)':
break;
}
}
return $items;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment