简体   繁体   English

httprequest异常处理php

[英]httprequest exception handling php

In case a request cannot be made, how can I catch the exception? 万一无法提出请求,我该如何捕捉异常?

I have so far written this, but not had any luck. 到目前为止,我已经写了这篇文章,但是没有任何运气。

$url = 'http://localhost:49000/';
//create the httprequest object                
try{
  $httpRequest_OBJ = new httpRequest($url, HTTP_METH_POST, $options);
}catch(HttpException $e){
  echo $e;
}

I had the same problem, you should make sure that the exception is in the namespace. 我有同样的问题,您应该确保该异常在名称空间中。

What I've done is: 我所做的是:

try {
    //Request here
} catch (\HttpException $e) {
    //Handle exception
} catch (\Exception $e) {
    //Just in case
}

Also good to notice what is pointed in this article http://www.mkfoster.com/2009/01/06/how-to-pecl-http-request-exception-and-error-handling/ about the inner exception, in case you want the message returned by the exception. 也很高兴注意到本文http://www.mkfoster.com/2009/01/06/how-to-pecl-http-request-exception-and-error-handling/中有关内部异常的内容,如果您想要异常返回的消息。

Also, there is a php.ini directive that forces HttpRequest to always throw exception http://br.php.net/manual/en/http.configuration.php http.only_exceptions=1 另外,有一个php.ini指令可强制HttpRequest始终抛出异常http://br.php.net/manual/en/http.configuration.php http.only_exceptions = 1

how can I catch the exception? 如何捕获异常?

To catch an exception you need to know the classname of the exception. 要捕获异常,您需要知道异常的类名。 What is the classname of the exception you'd like to catch? 您想捕获的异常的类名是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM