Created
October 21, 2015 15:35
-
-
Save karens/35c4a372ba79fc2997b9 to your computer and use it in GitHub Desktop.
drupal httpclient example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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