- 浏览: 2489849 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
PHP HTTP Library and Error Handling
Recently, I was using PHP code to do some HTTP protocol request. I have the Try Catch code there. But it seems it is not working well.
I finally find a solution and fix for the bug.
First of all, I am using a object oriented framework which mostly construct by my own. I just use a lot of open source things and putting together.
I have some codes like this in WebHttpClient.php
<?php
namespace JobConsumerPHP;
require __DIR__ . '/../../vendor/autoload.php';
use \GuzzleHttp\Client;
use \GuzzleHttp\Psr7\Request;
use \GuzzleHttp\Exception\ConnectException;
class WebHttpClient
{
private $classifierClient = null;
private $predictionClient = null;
private $geoServerClient = null;
private $ioc = null;
public function __construct($ioc)
{
$this->ioc = $ioc;
$logger = $this->ioc->getService("logger");
$config = $this->ioc->getService("config");
$logger->info("==============WebClient config start ==============");
$classifierURL = $config['classifierURL'];
$classifierKey = $config['classifierKey'];
$predictionURL = $config['predictionURL'];
$predictionKey = $config['predictionKey'];
$geoServerURL = $config['geoServerURL'];
$gatewayKey = $config['gatewayKey'];
$httpTimeout = $config['httpTimeout'];
$logger->info("classifierURL = {$classifierURL}");
$logger->info("classifierKey = {$classifierKey}");
$logger->info("predictionURL = {$predictionURL}");
$logger->info("predictionKey = {$predictionKey}");
$logger->info("predictionKey = {$predictionKey}");
$logger->info("httpTimeout = {$httpTimeout}");
$logger->info("=============================================");
try {
// init classifier HTTP
$this->classifierClient = new Client([
'base_uri' => $classifierURL,
'timeout' => $httpTimeout,
'connect_timeout' => $httpTimeout,
'http_errors' => false
]);
// init prediction HTTP
$this->predictionClient = new Client([
'base_uri' => $predictionURL,
'timeout' => $httpTimeout,
'connect_timeout' => $httpTimeout,
'headers' => [
'Content-Type' => 'application/json',
'x-api-key' => $predictionKey,
'api-gateway-key' => $gatewayKey
],
'http_errors' => false
]);
// init geo Server HTTP
$this->geoServerClient = new Client([
'base_uri' => $geoServerURL,
'timeout' => $httpTimeout,
'connect_timeout' => $httpTimeout,
'http_errors' => false
]);
} catch (\Exception $e) {
$logger->error("Couldn't init the HTTP Client.");
$logger->error($e->getMessage());
}
}
First of all, I have a namespace there, so I should always use \Exception, if I only put Exception there. The system can not catch the Exceptions. And the http_errors => false is also important.
If the HTTP method fail, the system need to retry that. So I have some codes like this
/**
* retry many times
*
* @param function $f
* @param number $delay
* @param number $retryies
*/
public function retry($f, $delay = 10, $retryies = 3)
{
$logger = $this->getService("logger");
try {
return $f();
} catch (\Exception $e) {
if ($retryies > 0) {
$logger->error("error happened " . $e->getMessage() . " system is retrying after " . $delay . " seconds" . " with the " . $retryies );
sleep($delay);
return $this->retry($f, $delay, $retryies - 1);
} else {
$logger->error($e->getMessage());
}
}
}
How we use the retry function is like this>
public function getFromClassifier($path)
{
return $this->ioc->retry(function () use ($path) {
$response = $this->classifierClient->request('GET', $path , array(
'http_errors' => false
));
return $response;
}, 10, 3);
}
I like this solutions. And maybe, we should use an random number there, system should retry in random time, not 10 seconds always.
References:
http://stackoverflow.com/questions/32252420/guzzle-curl-error-not-catche-by-try-catch-statement-laravel
https://github.com/gidkom/php-openfire-restapi/issues/3
http://stackoverflow.com/questions/17658283/catching-exceptions-from-guzzle
Recently, I was using PHP code to do some HTTP protocol request. I have the Try Catch code there. But it seems it is not working well.
I finally find a solution and fix for the bug.
First of all, I am using a object oriented framework which mostly construct by my own. I just use a lot of open source things and putting together.
I have some codes like this in WebHttpClient.php
<?php
namespace JobConsumerPHP;
require __DIR__ . '/../../vendor/autoload.php';
use \GuzzleHttp\Client;
use \GuzzleHttp\Psr7\Request;
use \GuzzleHttp\Exception\ConnectException;
class WebHttpClient
{
private $classifierClient = null;
private $predictionClient = null;
private $geoServerClient = null;
private $ioc = null;
public function __construct($ioc)
{
$this->ioc = $ioc;
$logger = $this->ioc->getService("logger");
$config = $this->ioc->getService("config");
$logger->info("==============WebClient config start ==============");
$classifierURL = $config['classifierURL'];
$classifierKey = $config['classifierKey'];
$predictionURL = $config['predictionURL'];
$predictionKey = $config['predictionKey'];
$geoServerURL = $config['geoServerURL'];
$gatewayKey = $config['gatewayKey'];
$httpTimeout = $config['httpTimeout'];
$logger->info("classifierURL = {$classifierURL}");
$logger->info("classifierKey = {$classifierKey}");
$logger->info("predictionURL = {$predictionURL}");
$logger->info("predictionKey = {$predictionKey}");
$logger->info("predictionKey = {$predictionKey}");
$logger->info("httpTimeout = {$httpTimeout}");
$logger->info("=============================================");
try {
// init classifier HTTP
$this->classifierClient = new Client([
'base_uri' => $classifierURL,
'timeout' => $httpTimeout,
'connect_timeout' => $httpTimeout,
'http_errors' => false
]);
// init prediction HTTP
$this->predictionClient = new Client([
'base_uri' => $predictionURL,
'timeout' => $httpTimeout,
'connect_timeout' => $httpTimeout,
'headers' => [
'Content-Type' => 'application/json',
'x-api-key' => $predictionKey,
'api-gateway-key' => $gatewayKey
],
'http_errors' => false
]);
// init geo Server HTTP
$this->geoServerClient = new Client([
'base_uri' => $geoServerURL,
'timeout' => $httpTimeout,
'connect_timeout' => $httpTimeout,
'http_errors' => false
]);
} catch (\Exception $e) {
$logger->error("Couldn't init the HTTP Client.");
$logger->error($e->getMessage());
}
}
First of all, I have a namespace there, so I should always use \Exception, if I only put Exception there. The system can not catch the Exceptions. And the http_errors => false is also important.
If the HTTP method fail, the system need to retry that. So I have some codes like this
/**
* retry many times
*
* @param function $f
* @param number $delay
* @param number $retryies
*/
public function retry($f, $delay = 10, $retryies = 3)
{
$logger = $this->getService("logger");
try {
return $f();
} catch (\Exception $e) {
if ($retryies > 0) {
$logger->error("error happened " . $e->getMessage() . " system is retrying after " . $delay . " seconds" . " with the " . $retryies );
sleep($delay);
return $this->retry($f, $delay, $retryies - 1);
} else {
$logger->error($e->getMessage());
}
}
}
How we use the retry function is like this>
public function getFromClassifier($path)
{
return $this->ioc->retry(function () use ($path) {
$response = $this->classifierClient->request('GET', $path , array(
'http_errors' => false
));
return $response;
}, 10, 3);
}
I like this solutions. And maybe, we should use an random number there, system should retry in random time, not 10 seconds always.
References:
http://stackoverflow.com/questions/32252420/guzzle-curl-error-not-catche-by-try-catch-statement-laravel
https://github.com/gidkom/php-openfire-restapi/issues/3
http://stackoverflow.com/questions/17658283/catching-exceptions-from-guzzle
发表评论
-
PHP Command Line Tool
2019-08-17 05:06 359PHP Command Line Tool Recently ... -
Code SonarQube 2019(2)PostgreSQL as Database
2019-07-24 05:32 379Code SonarQube 2019(2)PostgreSQ ... -
Code SonarQube 2019(1)Installation with default H2
2019-07-23 10:42 739Code SonarQube 2019(1)Installat ... -
Auth Solution(3)JWT in Java and PHP Sample
2019-05-03 07:33 286Auth Solution(3)JWT in Java and ... -
Flarum BBS System(2)Docker the BBS
2018-11-20 03:31 607Flarum BBS System(2)Docker the ... -
Flarum BBS System(1)Installation and Introduction
2018-11-18 14:29 464Flarum BBS System(1)Installatio ... -
Grav CMS System(5)Multiple Domains and Certs
2018-11-15 01:48 497Grav CMS System(5)Multiple Doma ... -
Grav CMS System(4)Multiple Domain Sites in HAProxy and HTTPS
2018-11-14 21:33 554Grav CMS System(4)Multiple Doma ... -
Grav CMS System(3)Docker Nginx and PHP
2018-11-14 00:21 430Grav CMS System(3)Docker Nginx ... -
Grav CMS System(1)Install PHP7 on MAC
2018-10-23 21:53 544Grav CMS System(1)Install PHP7 ... -
Laravel PHP Framework(1)Introduction and Installation
2018-08-22 02:47 956Laravel PHP Framework(1)Introdu ... -
JSON Log in PHP and Cloud Watch(1)JSON Format in PHP
2017-12-30 05:31 566JSON Log in PHP and Cloud Watch ... -
PHPRedis Library
2017-03-30 00:32 645PHPRedis Library We can instal ... -
PHP Call Wget Command
2017-03-24 23:35 642PHP Call Wget Command In my PH ... -
PHP XML Writer
2017-03-10 03:59 1426PHP XML Writer PHP XML writer ... -
PHP Redis Client and Cluster
2017-02-16 03:34 616PHP Redis Client and Cluster S ... -
PHP Redis Client and Replica
2017-02-16 01:11 558PHP Redis Client and Replica W ... -
PHP ENV and HTTP Extension
2017-01-12 01:04 763PHP ENV and HTTP Extension We ... -
PHP Connect Redis Driver
2016-11-23 00:29 550PHP Connect Redis Driver I was ... -
Batch Insert in PHP MySQLi
2016-08-24 00:58 668Batch Insert in PHP MySQLi Whe ...
相关推荐
Transparent output compression using the zlib library ; Valid values for this option are 'off', 'on', or a specific buffer size ; to be used for compression (default is 4KB) ; Note:...
Error Handling and Logging Functions XXXIII. Exif Functions XXXIV. Expect Functions XXXV. File Alteration Monitor Functions XXXVI. Forms Data Format Functions XXXVII. filePro Functions XXXVIII. ...
Error Handling and Logging Functions XXVII. File Alteration Monitor Functions XXVIII. FrontBase Functions XXIX. filePro Functions XXX. 文件系统函数库 XXXI. Forms Data Format Functions XXXII. FriBiDi ...
Errors and Logging — Error Handling and Logging Functions Exif — Exif Functions Expect — Expect Functions fam — File Alteration Monitor Functions FDF — Forms Data Format Functions Fileinfo — ...
Error Handling and Logging Functions XXXII. Exif Functions XXXIII. File Alteration Monitor Functions XXXIV. filePro Functions XXXV. Filesystem 文件系统函数 XXXVI. Firebird/InterBase Functions XXXVII....
Error Handling and Logging Functions XXXII. Exif Functions XXXIII. File Alteration Monitor Functions XXXIV. filePro Functions XXXV. Filesystem 文件系统函数 XXXVI. Firebird/InterBase Functions XXXVII....
Error Handling and Logging Functions XXXII. Exif Functions XXXIII. File Alteration Monitor Functions XXXIV. filePro Functions XXXV. Filesystem 文件系统函数 XXXVI. Firebird/InterBase Functions XXXVII....
Error Handling 13 The Guestbook Application 13 Program Summary 13 Primary Code Listing 14 From the Library of Lee Bogdanoff CONTENTS vi Users Do the Darnedest Things . . . 15 I Wonder What Will Happen...
Error Handling and Logging Functions XXXV. Exif Functions XXXVI. Expect Functions XXXVII. File Alteration Monitor Functions XXXVIII. Forms Data Format Functions XXXIX. Fileinfo Functions XL. filePro ...
Error Handling and Logging Functions XXXV. Exif Functions XXXVI. Expect Functions XXXVII. File Alteration Monitor Functions XXXVIII. Forms Data Format Functions XXXIX. Fileinfo Functions XL. ...
Error Handling and Logging Functions XXXII. Exif Functions XXXIII. File Alteration Monitor Functions XXXIV. filePro Functions XXXV. Filesystem 文件系统函数 XXXVI. Firebird/InterBase Functions ...
Errors and Logging — Error Handling and Logging Functions Exif — Exif Functions Expect — Expect Functions fam — File Alteration Monitor Functions FDF — Forms Data Format Functions Fileinfo — ...
Error Handling and Logging Functions XXXIII. Exif Functions XXXIV. Expect Functions XXXV. File Alteration Monitor Functions XXXVI. Forms Data Format Functions XXXVII. filePro Functions XXXVIII. ...
Error Handling and Logging Functions XXXII. Exif Functions XXXIII. File Alteration Monitor Functions XXXIV. filePro Functions XXXV. Filesystem 文件系统函数 XXXVI. Firebird/InterBase Functions XXXVII....
Error Handling and Logging Functions XXXII. Exif Functions XXXIII. File Alteration Monitor Functions XXXIV. filePro Functions XXXV. Filesystem 文件系统函数 XXXVI. Firebird/InterBase Functions XXXVII....
Error Handling and Logging Functions XXXII. Exif Functions XXXIII. File Alteration Monitor Functions XXXIV. filePro Functions XXXV. Filesystem 文件系统函数 XXXVI. Firebird/InterBase Functions XXXVII....
Chapter 11: Error Handling and Reporting 279 Raising an error-level J!error 281 Raising a warning-level J!error 283 Raising a notice-level J!error 285 Enqueuing a message 287 Changing the default...
Error Handling 420 Error-Handling Exceptions 421 02_776130 ftoc.qxp 2/2/07 10:13 PM Page xvi xvii Contents Optimization Techniques 422 Installing Additional PHP Software 427 Logging 427 Parameter ...
http://wiki.overbyte.be/wiki/index.php/ICS_Download ICS V5 and V6 are archive releases no longer updated, last supported release was 2007. ICS V7 is a stable release that may still be updated for ...
- also see FAQ: http://www.wpcubed.com/forum/viewforum.php?f=15 - and support forum: http://www.wpcubed.com/forum/ - WPTools 6 PRO and PREMIUM will be compiled as Version 5 if $define "WP6" is not set...