Skip to content

Instantly share code, notes, and snippets.

@benleivian
Created February 24, 2016 18:46
Show Gist options
  • Save benleivian/3ded690211fdf96d21f6 to your computer and use it in GitHub Desktop.
Save benleivian/3ded690211fdf96d21f6 to your computer and use it in GitHub Desktop.
Cache guzzle request
<?php
public function getRecords($id)
{
$cache_name = 'records_' . $id;
$cache = new FileStore(new Filesystem($cache_name . '.txt'), __DIR__ . '/cache');
// If cache exists
if ($cache->get($cache_name)) {
return $cache->get($cache_name);
} else {
try {
// Try to get records
$client = new GuzzleHttp('https://api.hello.com/records/1399394?access_token=w3r2232r');
$request = $client->get()->send();
$records = json_decode($request->getBody(), true);
// Save records in cache
$cache->put($cache_name, $records, 600);
return $records;
} catch (GuzzleHttpExceptionBadResponseException $e) {
$raw_response = explode("n", $e->getResponse());
throw new IDPException(end($raw_response));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment