- 浏览: 2539322 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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 386PHP Command Line Tool Recently ... -
Code SonarQube 2019(2)PostgreSQL as Database
2019-07-24 05:32 417Code SonarQube 2019(2)PostgreSQ ... -
Code SonarQube 2019(1)Installation with default H2
2019-07-23 10:42 764Code SonarQube 2019(1)Installat ... -
Auth Solution(3)JWT in Java and PHP Sample
2019-05-03 07:33 306Auth Solution(3)JWT in Java and ... -
Flarum BBS System(2)Docker the BBS
2018-11-20 03:31 643Flarum BBS System(2)Docker the ... -
Flarum BBS System(1)Installation and Introduction
2018-11-18 14:29 496Flarum BBS System(1)Installatio ... -
Grav CMS System(5)Multiple Domains and Certs
2018-11-15 01:48 526Grav CMS System(5)Multiple Doma ... -
Grav CMS System(4)Multiple Domain Sites in HAProxy and HTTPS
2018-11-14 21:33 572Grav CMS System(4)Multiple Doma ... -
Grav CMS System(3)Docker Nginx and PHP
2018-11-14 00:21 454Grav CMS System(3)Docker Nginx ... -
Grav CMS System(1)Install PHP7 on MAC
2018-10-23 21:53 612Grav CMS System(1)Install PHP7 ... -
Laravel PHP Framework(1)Introduction and Installation
2018-08-22 02:47 978Laravel PHP Framework(1)Introdu ... -
JSON Log in PHP and Cloud Watch(1)JSON Format in PHP
2017-12-30 05:31 596JSON Log in PHP and Cloud Watch ... -
PHPRedis Library
2017-03-30 00:32 672PHPRedis Library We can instal ... -
PHP Call Wget Command
2017-03-24 23:35 664PHP Call Wget Command In my PH ... -
PHP XML Writer
2017-03-10 03:59 1446PHP XML Writer PHP XML writer ... -
PHP Redis Client and Cluster
2017-02-16 03:34 642PHP Redis Client and Cluster S ... -
PHP Redis Client and Replica
2017-02-16 01:11 588PHP Redis Client and Replica W ... -
PHP ENV and HTTP Extension
2017-01-12 01:04 787PHP ENV and HTTP Extension We ... -
PHP Connect Redis Driver
2016-11-23 00:29 573PHP Connect Redis Driver I was ... -
Batch Insert in PHP MySQLi
2016-08-24 00:58 696Batch 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:...
- **HTTP Requests:** Description of sending HTTP requests using PHP, including GET and POST requests. - **Consuming JSON/XML Data:** Techniques for consuming and processing JSON and XML data in PHP. -...
1. **Error Handling**:PHP 5.3引入了异常处理机制,代替了之前的错误报告方式。这意味着旧的错误处理函数如`@`操作符来抑制错误不再推荐使用,而是应该抛出和捕获异常。 2. **魔术常量**:`__FUNCTION__`, `__...
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. ...
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 ...
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 XXVII. File Alteration Monitor Functions XXVIII. FrontBase Functions XXIX. filePro Functions XXX. 文件系统函数库 XXXI. Forms Data Format Functions XXXII. FriBiDi ...
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 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. ...
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....
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 ...
5. **Error Handling**:理解和处理API调用可能出现的错误,如网络问题、权限错误或API限制。 6. **Pagination**:如果API返回大量数据,了解如何使用分页处理结果。 7. **Caching**:为提高性能,了解如何使用缓存...