Last active
April 4, 2020 14:37
-
-
Save marcus-at-localhost/f116b5808f87000e374ddec5b07f530e to your computer and use it in GitHub Desktop.
[Cache and Log Guzzle Requests] #guzzle #cache #log
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
<?php | |
use GuzzleHttp\Client; | |
use GuzzleHttp\HandlerStack; | |
use GuzzleHttp\MessageFormatter; | |
use GuzzleHttp\Middleware; | |
use GuzzleHttp\Promise; | |
/** | |
"kevinrob/guzzle-cache-middleware": "^3.2", | |
"symfony/cache": "^3.4", | |
"monolog/monolog": "^1.24.0" | |
*/ | |
use Kevinrob\GuzzleCache\CacheMiddleware; | |
use Kevinrob\GuzzleCache\Storage\Psr6CacheStorage; | |
use Symfony\Component\Cache\Adapter\FilesystemAdapter; | |
use Psr\Http\Message\ResponseInterface; | |
use Psr\Http\Message\RequestInterface; | |
use Monolog\Logger; | |
use Monolog\Handler\RotatingFileHandler; | |
// Create default HandlerStack | |
$stack = HandlerStack::create(); | |
/** | |
* @link https://stackoverflow.com/questions/32681165/how-do-you-log-all-api-calls-using-guzzle-6 | |
* @link https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php#L12 | |
* @var Logger | |
*/ | |
$logger = new Logger('guzzle'); | |
$handler = new RotatingFileHandler(ROOT_PATH . '/cache/log.log', 0, Logger::DEBUG, true, 0664); | |
$handler->setFilenameFormat('{filename}-{date}', 'Ymd'); | |
$logger->pushHandler($handler); | |
$stack->push( | |
Middleware::log( | |
$logger, | |
// https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php#L12 | |
new MessageFormatter('{method} {target} - {res_headers}') | |
), | |
'logger' | |
); | |
/** | |
* set caching | |
* | |
* The `PrivateCacheStrategy` will cache private and public responses | |
* The `PublicCacheStrategy` will only cache responses suitable for a public-cache | |
* The `GreedyCacheStrategy` will cache everything - even if the header does not allow any caching at all. | |
* | |
* @link https://github.com/Kevinrob/guzzle-cache-middleware | |
* @link https://web.archive.org/web/20180907070705/https://a.kabachnik.info/caching-http-requests-with-guzzle-6-and-psr-6.html | |
*/ | |
$stack->push( | |
new CacheMiddleware( | |
#new \Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy( | |
new \Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy( | |
new Psr6CacheStorage( | |
new FilesystemAdapter('guzzle', 0, ROOT_PATH.'/cache/') | |
), | |
60*15 | |
) | |
), | |
'cache' | |
); | |
// $stack->remove('logger'); | |
// @link http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html#handlerstack | |
// Initialize the client with the handler option | |
$client = new Client(['handler' => $stack]); | |
// remove 'logger' from the handler stack after initialization | |
$client->getConfig('handler')->remove('logger'); | |
// do $client->get('https://what.ever/api/hi.json'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment