Search
 
SCRIPT & CODE EXAMPLE
 

PHP

guzzle http try catch

try {
    /**
     * We use Guzzle to make an HTTP request somewhere in the
     * following theMethodMayThrowException().
     */
    $result = theMethodMayThrowException();
} catch (GuzzleHttpExceptionRequestException $e) {
    /**
     * Here we actually catch the instance of GuzzleHttpPsr7Response
     * (find it in ./vendor/guzzlehttp/psr7/src/Response.php) with all
     * its own and its 'Message' trait's methods. See more explanations below.
     *
     * So you can have: HTTP status code, message, headers and body.
     * Just check the exception object has the response before.
     */
    if ($e->hasResponse()) {
        $response = $e->getResponse();
        var_dump($response->getStatusCode()); // HTTP status code;
        var_dump($response->getReasonPhrase()); // Response message;
        var_dump((string) $response->getBody()); // Body, normally it is JSON;
        var_dump(json_decode((string) $response->getBody())); // Body as the decoded JSON;
        var_dump($response->getHeaders()); // Headers array;
        var_dump($response->hasHeader('Content-Type')); // Is the header presented?
        var_dump($response->getHeader('Content-Type')[0]); // Concrete header value;
    }
}
// process $result etc. ...
Comment

PREVIOUS NEXT
Code Example
Php :: php artisan make migration 
Php :: php get myme type of image 
Php :: increment single column laravel 
Php :: get_template_part parameters 
Php :: php get hour 
Php :: php truncate string 
Php :: Script timeout passed, if you want to finish import 
Php :: laravel vendor publish all files 
Php :: set font sytle phpspreadsheet 
Php :: php default timezone 
Php :: display exception in blade laravel 
Php :: get logged in user name yii2 
Php :: get post title by post id wordpress 
Php :: call table name in model laravel 
Php :: laravel request validation boolean 
Php :: excerpt length wordpress 
Php :: yii2 redirect with 301 
Php :: year in php 
Php :: switch php version 
Php :: laravel now date 
Php :: twig for 
Php :: php string replace space 
Php :: php datetime create 
Php :: get message validator failed laravel 
Php :: customer io send_at api 
Php :: php string starts with 
Php :: run artisan command from controller 
Php :: laravel session put method 
Php :: install symfony ubuntu 
Php :: kill laravel server 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =