Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
Ready to use (hopefully\!)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoleary committed Sep 8, 2015
1 parent ea881d1 commit 95d1d9d
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 9 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
composer.phar
vendor/
data/*

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
Expand Down
49 changes: 43 additions & 6 deletions classes/HttpPostTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,32 @@

namespace HttpPostTester;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use GuzzleHttp;
use Teapot\StatusCode;

class HttpPostTester
{
const EXPECTED_RESPONSE = 'OK';

protected $strPathToData = __DIR__ . '/../data/';

protected $objLogger;

public function __construct()
{

date_default_timezone_set('Europe/London');

$this->objLogger = new Logger(__CLASS__);
$this->objLogger->pushHandler(new StreamHandler(sprintf('%s/../logs/results_%s.log', __DIR__, date('YmdHis')), Logger::WARNING));
$this->objLogger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
}

public function init()
{
$this->objLogger->addDebug('Started!');

$resHandle = opendir($this->strPathToData);

while (false !== ($strFile = readdir($resHandle))) {
Expand All @@ -24,20 +39,42 @@ public function init()
continue;
}

if (false !== ($resFileHandle = fopen($this->strPathToData, 'r'))) {
if (false !== ($resFileHandle = fopen($this->strPathToData . $strFile, 'r'))) {
while (false !== ($arrRow = fgetcsv($resFileHandle))) {
$this->doHttpPost($arrRow[0]);
}

fclose($handle);
fclose($resFileHandle);
}
}

$this->objLogger->addDebug('Finished!');
}

public function doHttpPost($strUrl)
{
// $objGuzzle = new Guzzle\Guzzle();
$objHttp = new GuzzleHttp\Client();

$objResult = $objHttp->post($strUrl, [
GuzzleHttp\RequestOptions::ALLOW_REDIRECTS => true,
GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => 5,
GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
GuzzleHttp\RequestOptions::QUERY => []
]);

if (StatusCode::OK === (int) $objResult->getStatusCode()) {
$this->objLogger->addDebug(sprintf('[Result_OK] 200 OK from %s', $strUrl), [
'body' => (string) $objResult->getBody()
]);


if (0 !== stripos((string) $objResult->getBody(), self::EXPECTED_RESPONSE)) {
$this->objLogger->addWarning(sprintf('[Result_WARN] %d but not OK from %s', $objResult->getStatusCode(), $strUrl), [
'body' => (string) $objResult->getBody()
]);
}
} else {
$this->objLogger->addCritical(sprintf('[Result_CRIT] %d from %s', $objResult->getStatusCode(), $strUrl), [
'body' => (string) $objResult->getBody()
]);
}
}
}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
],
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.0"
"guzzlehttp/guzzle": "^6.0",
"shrikeh/teapot": "^1.0",
"monolog/monolog": "^1.17"
},
"autoload": {
"psr-4": {
Expand Down
231 changes: 230 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 95d1d9d

Please sign in to comment.