PHP code example of webignition / http-history-container
1. Go to this page and download the library: Download webignition/http-history-container library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
webignition / http-history-container example snippets
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use webignition\HttpHistoryContainer\Container;
$historyContainer = new Container();
$historyHandler = Middleware::history($historyContainer);
$handlerStack = HandlerStack::create($historyHandler);
$httpClient = new HttpClient(['handler' => $handlerStack]);
/// ... things happen ...
$historyContainer->getTransactions();
// an array of HttpTransaction
$historyContainer->getRequests();
// a Collection\RequestCollection
$historyContainer->getResponses();
// a Collection\ResponseCollection
foreach ($historyContainer as $transaction) {
// $transaction is a HttpTransaction
$request = $transaction->getRequest();
// $request is a RequestInterface
$response = $transaction->getResponse();
// $response is a ResponseInterface
}
use webignition\HttpHistoryContainer\Container;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
$historyContainer = new Container();
// ... create and use Guzzle client ...
$transactions = $historyContainer->getTransactions();
// $transactions is a Collection\TransactionCollection
$requests = $transactions->getRequests();
// $requests is a Collection\RequestCollection
// RequestCollection is iterable
foreach ($requests as $request) {
// $request is a RequestInterface
}
$urls = $requests->getUrls();
// $urls is a Collection\UrlCollection
// UrlCollection is iterable
foreach ($urls as $url) {
// $url is a UriInterface
}
foreach ($urls->getAsStrings() as $urlString) {
// convenient access to requested URLs as strings
}
use webignition\HttpHistoryContainer\Container;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
$historyContainer = new Container();
// ... create and use Guzzle client ...
$transactions = $historyContainer->getTransactions();
// $transactions is a Collection\TransactionCollection
$responses = $transactions->getResponses();
// $responses is a Collection\ResponseCollection
// ResponseCollection is iterable
foreach ($responses as $response) {
// $response is a ResponseInterface
}
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use webignition\HttpHistoryContainer\LoggableContainer;
use webignition\HttpHistoryContainer\Transaction\LoggableTransaction;
// Create the PSR logger
$path = '/path/to/log';
$stream = fopen($path, 'w+');
$logger = new Logger('');
$logHandler = new StreamHandler($stream);
$logHandler
->setFormatter(new LineFormatter('%message%' . "\n"));
$logger->pushHandler($logHandler);
// Create LoggableContainer
$container = new LoggableContainer($logger);
// ... create and use Guzzle client ...
$logContent = file_get_contents($path);
$logLines = array_filter(explode("\n", $logContent));
$loggedTransactions = [];
foreach ($logLines as $logLine) {
$loggedTransactions[] = LoggableTransaction::fromJson($logLine);
}
// $loggedTransactions is now an array of LoggableTransaction
foreach ($loggedTransactions as $loggedTransaction) {
$transaction = $loggedTransaction->getTransaction();
// $transaction is a HttpTransaction
$request = $transaction->getRequest();
// $request is a RequestInterface
$response = $transaction->getResponse();
// $response is a ResponseInterface
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.