Created
February 24, 2016 18:46
-
-
Save benleivian/3ded690211fdf96d21f6 to your computer and use it in GitHub Desktop.
Cache guzzle request
This file contains hidden or 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
<?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