mercredi 18 octobre 2017

Make a GET with Guzzle in Laravel 5.1

I have an API, and make GET to it via Postmen

Ex.http://site/api/users.count

I got

{
    "status": 200,
    "message": "Success",
    "data": {
        "count": 8
    }
}

I've tried to use Guzzle

composer require guzzlehttp/guzzle  


Using version ^6.3 for guzzlehttp/guzzle                                                                                          
./composer.json has been updated                                                                                                  
Loading composer repositories with package information                                                                            
Updating dependencies (including require-dev)                                                                                     
  - Installing guzzlehttp/promises (v1.3.1)                                                                                       
    Downloading: 100%                                                                                                             

  - Installing psr/http-message (1.0.1)                                                                                           
    Loading from cache                                                                                                            

  - Installing guzzlehttp/psr7 (1.4.2)                                                                                            
    Loading from cache                                                                                                            

  - Installing guzzlehttp/guzzle (6.3.0)                                                                                          
    Downloading: 100%                                                                                                             

Writing lock file                                                                                                                 
Generating autoload files                                                                                                         
> php artisan clear-compiled                                                                                                      

> php artisan optimize                                                                                                            

Generating optimized class loader                                                                                                 


include it

I add these 2 lines on top of my class

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;


use it

$client = new Client();
$res = $client->request('GET','http://site/api/users.count');
dd($res);


result

I kept getting

Response {#664 ▼
  -reasonPhrase: "OK"
  -statusCode: 200
  -headers: array:4 [▼
    "Connection" => array:1 [▼
      0 => "Keep-Alive"
    ]
    "Content-Length" => array:1 [▼
      0 => "61"
    ]
    "Content-Type" => array:1 [▼
      0 => "application/json; charset=utf-8"
    ]
    "Date" => array:1 [▼
      0 => "Wed, 18 Oct 2017 18:01:50 GMT"
    ]
  ]
  -headerNames: array:4 [▼
    "connection" => "Connection"
    "content-length" => "Content-Length"
    "content-type" => "Content-Type"
    "date" => "Date"
  ]
  -protocol: "1.1"
  -stream: Stream {#662 ▼
    -stream: stream resource @272 ▼
      wrapper_type: "PHP"
      stream_type: "TEMP"
      mode: "w+b"
      unread_bytes: 0
      seekable: true
      uri: "php://temp"
      options: []
    }
    -size: null
    -seekable: true
    -readable: true
    -writable: true
    -uri: "php://temp"
    -customMetadata: []
  }
}


expected result

I am hoping to get a similar result like this :

{
    "status": 200,
    "message": "Success",
    "data": {
        "count": 8
    }
}


question

How would one go about and debug this further ?


I'm open to any suggestions at this moment.

Any hints/suggestions / help on this be will be much appreciated!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire