From 95d1d9d0b6847d2125f63029fa5f02e2b3e899bf Mon Sep 17 00:00:00 2001 From: Marc O'Leary Date: Tue, 8 Sep 2015 10:33:31 +0100 Subject: [PATCH] Ready to use (hopefully\!) --- .gitignore | 1 - classes/HttpPostTester.php | 49 +++++++- composer.json | 4 +- composer.lock | 231 ++++++++++++++++++++++++++++++++++++- 4 files changed, 276 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index db9d44c..3a693c9 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/classes/HttpPostTester.php b/classes/HttpPostTester.php index 57e7b22..108061d 100644 --- a/classes/HttpPostTester.php +++ b/classes/HttpPostTester.php @@ -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))) { @@ -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() + ]); + } } } diff --git a/composer.json b/composer.json index 3c7319e..e3eec80 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/composer.lock b/composer.lock index d69459e..1a70504 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "09fd45f1963aa31489b03b138ea7c6e3", + "hash": "f37c183e8418ee75662d711c7b30a7a8", "packages": [ { "name": "guzzlehttp/guzzle", @@ -177,6 +177,82 @@ ], "time": "2015-08-15 19:32:36" }, + { + "name": "monolog/monolog", + "version": "1.17.1", + "source": { + "type": "git", + "url": "https://github.com/Seldaek/monolog.git", + "reference": "0524c87587ab85bc4c2d6f5b41253ccb930a5422" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/0524c87587ab85bc4c2d6f5b41253ccb930a5422", + "reference": "0524c87587ab85bc4c2d6f5b41253ccb930a5422", + "shasum": "" + }, + "require": { + "php": ">=5.3.0", + "psr/log": "~1.0" + }, + "provide": { + "psr/log-implementation": "1.0.0" + }, + "require-dev": { + "aws/aws-sdk-php": "^2.4.9", + "doctrine/couchdb": "~1.0@dev", + "graylog2/gelf-php": "~1.0", + "php-console/php-console": "^3.1.3", + "phpunit/phpunit": "~4.5", + "phpunit/phpunit-mock-objects": "2.3.0", + "raven/raven": "~0.11", + "ruflin/elastica": ">=0.90 <3.0", + "swiftmailer/swiftmailer": "~5.3", + "videlalvaro/php-amqplib": "~2.4" + }, + "suggest": { + "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", + "doctrine/couchdb": "Allow sending log messages to a CouchDB server", + "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", + "ext-mongo": "Allow sending log messages to a MongoDB server", + "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server", + "php-console/php-console": "Allow sending log messages to Google Chrome", + "raven/raven": "Allow sending log messages to a Sentry server", + "rollbar/rollbar": "Allow sending log messages to Rollbar", + "ruflin/elastica": "Allow sending log messages to an Elastic Search server", + "videlalvaro/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.16.x-dev" + } + }, + "autoload": { + "psr-4": { + "Monolog\\": "src/Monolog" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "http://seld.be" + } + ], + "description": "Sends your logs to files, sockets, inboxes, databases and various web services", + "homepage": "http://github.com/Seldaek/monolog", + "keywords": [ + "log", + "logging", + "psr-3" + ], + "time": "2015-08-31 09:17:37" + }, { "name": "psr/http-message", "version": "1.0", @@ -225,6 +301,159 @@ "response" ], "time": "2015-05-04 20:22:00" + }, + { + "name": "psr/log", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "fe0936ee26643249e916849d48e3a51d5f5e278b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/fe0936ee26643249e916849d48e3a51d5f5e278b", + "reference": "fe0936ee26643249e916849d48e3a51d5f5e278b", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-0": { + "Psr\\Log\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "time": "2012-12-21 11:40:51" + }, + { + "name": "shrikeh/teapot", + "version": "v1.0.2", + "source": { + "type": "git", + "url": "https://github.com/shrikeh/teapot.git", + "reference": "879ad8e197b75d0a34afb92e0ec70b75fd5cf171" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/shrikeh/teapot/zipball/879ad8e197b75d0a34afb92e0ec70b75fd5cf171", + "reference": "879ad8e197b75d0a34afb92e0ec70b75fd5cf171", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "squizlabs/php_codesniffer": "^2.3" + }, + "type": "library", + "autoload": { + "psr-0": { + "Teapot": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Barney Hanlon", + "email": "barney@shrikeh.net" + } + ], + "description": "PHP HTTP Response Status library", + "homepage": "http://shrikeh.github.com/teapot", + "keywords": [ + "http" + ], + "time": "2015-08-17 09:36:20" + }, + { + "name": "squizlabs/php_codesniffer", + "version": "2.3.3", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "c1a26c729508f73560c1a4f767f60b8ab6b4a666" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/c1a26c729508f73560c1a4f767f60b8ab6b4a666", + "reference": "c1a26c729508f73560c1a4f767f60b8ab6b4a666", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.1.2" + }, + "bin": [ + "scripts/phpcs", + "scripts/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "CodeSniffer.php", + "CodeSniffer/CLI.php", + "CodeSniffer/Exception.php", + "CodeSniffer/File.php", + "CodeSniffer/Fixer.php", + "CodeSniffer/Report.php", + "CodeSniffer/Reporting.php", + "CodeSniffer/Sniff.php", + "CodeSniffer/Tokens.php", + "CodeSniffer/Reports/", + "CodeSniffer/Tokenizers/", + "CodeSniffer/DocGenerators/", + "CodeSniffer/Standards/AbstractPatternSniff.php", + "CodeSniffer/Standards/AbstractScopeSniff.php", + "CodeSniffer/Standards/AbstractVariableSniff.php", + "CodeSniffer/Standards/IncorrectPatternException.php", + "CodeSniffer/Standards/Generic/Sniffs/", + "CodeSniffer/Standards/MySource/Sniffs/", + "CodeSniffer/Standards/PEAR/Sniffs/", + "CodeSniffer/Standards/PSR1/Sniffs/", + "CodeSniffer/Standards/PSR2/Sniffs/", + "CodeSniffer/Standards/Squiz/Sniffs/", + "CodeSniffer/Standards/Zend/Sniffs/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "http://www.squizlabs.com/php-codesniffer", + "keywords": [ + "phpcs", + "standards" + ], + "time": "2015-06-24 03:16:23" } ], "packages-dev": [],