spreaderman
spreaderman

Reputation: 1086

Guzzle 6.x / Not getting expected result

I have a restful API. when I run POSTMAN on the following URL, I receive as response as follows:

POSTMAN RUN URL
DELETE
https://www.example.com/api/v1/Blog/blog/13


      {
      "status":"Failure",
      "message":"The specified blog post could not be found"
      }

The above is of course expected, however, I am unable to read in "status" and "message". How do I get that reply? Here is my present code:

      $entry_id = $this->uri->segment(3);
      $theUrl = $this->config->item('base_url').'api/v1/Blog/blog/'.$entry_id;
      // tested $theUrl and works 
      $client = new GuzzleHttp\Client([
        'base_uri' =>       $theUrl,
        'timeout'  =>       3.0,
        'http_errors' =>    FALSE
        ]);
      $response = $client->delete($theUrl);
      $code = $response->getStatusCode();
      $response = $client->delete($theUrl); 
      $x = $response->getBody();

 echo "<pre>";
 echo var_dump($x);  // cannot see message or status anywhere.
 echo "</pre>";

Your suggestions much appreciated.

+++ I have now tried this revised code but still cannot see the STATUS or MESSAGE date in the reply:

    $entry_id = $this->uri->segment(3);

    $theUrl = $this->config->item('base_url').'api/v1/Blog/blog/'.$entry_id;
    $client = new GuzzleHttp\Client([
        'timeout'  =>       3.0,
        'http_errors' =>    FALSE
        ]);
    $response = $client->delete($theUrl, ['debug' => true]);
    $code = $response->getStatusCode();
    $x = $response->getBody();

    echo "<pre>";
    echo var_dump($x);
    echo "</pre>";

    die();

Here is the dump and the debug info:

 https://www.example.com/api/v1/Blog/blog/6
 * Hostname was found in DNS cache * Trying 104.131.132.25... * Connected to      
 www.example.com (104.131.132.25) port 443 (#1) * successfully set  
 certificate verify locations: * CAfile: none CApath: /etc/ssl/certs * SSL 
 connection using XXXXXXXXXXXXXXXXXXXXXXXXX* Server certificate: * 
 subject: CN=www.example.com * start date: 2016-10-29 05:15:00 GMT * expire 
 date: 2017-01-27 05:15:00 GMT * subjectAltName: www.movinghaus.com matched 
 * issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3 * SSL 
 certificate verify ok. > DELETE /api/v1/Blog/blog/6 HTTP/1.1 User-Agent: 
 GuzzleHttp/6.2.0 curl/7.35.0 PHP/5.5.9-1ubuntu4.11 Host: www.example.com < 
 HTTP/1.1 200 OK < Date: Fri, 04 Nov 2016 03:55:21 GMT * Server Apache/2.4.7 
 (Ubuntu) is not blacklisted < Server: Apache/2.4.7 (Ubuntu) < X-Powered-By: 
 PHP/5.5.9-1ubuntu4.11 < Set-Cookie: 
 PHPSESSID=XXXXXXXXXXXXXXXXXX; expires=Fri, 04-Nov-2016 05:55:21 GMT; Max-
 Age=7200; path=/; HttpOnly < Expires: Thu, 19 Nov 1981 08:52:00 GMT < 
 Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-
 check=0 < Pragma: no-cache < Content-Length: 40 < Content-Type: 
 application/json; charset=utf-8 < * Connection #1 to host www.example.com 
 left intact


 object(GuzzleHttp\Psr7\Stream)#65 (7) {
 ["stream":"GuzzleHttp\Psr7\Stream":private]=>
 resource(50) of type (stream)
 ["size":"GuzzleHttp\Psr7\Stream":private]=>
 NULL
 ["seekable":"GuzzleHttp\Psr7\Stream":private]=>
 bool(true)
 ["readable":"GuzzleHttp\Psr7\Stream":private]=>
 bool(true)
 ["writable":"GuzzleHttp\Psr7\Stream":private]=>
 bool(true)
 ["uri":"GuzzleHttp\Psr7\Stream":private]=>
 string(10) "php://temp"
 ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
 array(0) {
 }
 }

Upvotes: 0

Views: 317

Answers (2)

Stafox
Stafox

Reputation: 1005

In according to docs. You should do

$x = $response->getBody()->getContents();

Or cast body to string:

$x = (string)$response->getBody()

Upvotes: 0

Nadir Latif
Nadir Latif

Reputation: 3773

I think the base_uri parameter for new GuzzleHttp\Client is not needed. The base_uri parameter is used to set a base url. (http://docs.guzzlephp.org/en/latest/quickstart.html?highlight=base_uri) All calls to the client should then use relative uris. Since you are using an absolute url in the delete function call, you should not need the base_uri parameter.

Also you are calling the delete function twice: $response = $client->delete($theUrl).

Upvotes: 1

Related Questions