Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mhdd's avatar
Level 9

guzzle won't retrieve json data.

Hello guys.

I created an simple rest API with laravel 5.3. the scenario is so simple get a token then response a JSON data(ex. User data based on token.) when i send the request from postman i am getting my data completely as JSON. lets call this User Management.

ex:

{
  "email": "[email protected]",
  "username": "cruelcap",
  "password": "$2y$10$rqL.8EQOz3qILlD6BdCZH.3X7eIULC6QcD7mljzf9ozg.6HGkGG1y",
  "phone_number": "09388478675",
  "activated": 1,
  "banned": 0,
  "email_verified": 1,
  "remember_token": null,
  "created_at": "2016-12-18 14:44:09",
  "updated_at": "2016-12-18 14:44:09",
  "last_activity": "2016-12-25 13:28:14"
}

now i created another laravel api (5.3) that his job is sending a request to User Management. i am sending my request with guzzle like this.

$header = [
       'Content-Type' => 'application/json',
       'Accept' => 'application/json',
       'Authorization' => 'Bearer ' . $token,
 ];

 $url = 'http://user-management.dev/api/login';

 $client = new Client([
        'headers' => $header
 ]);

$response = $client->request('POST', $url);

dd($response);

the problem is i am not getting anything from the json data. i am getting a typical guzzle instance & request header & etc.

nothing from the json data.

here is the output of my secound api:

Response {#166
  -reasonPhrase: "OK"
  -statusCode: 200
  -headers: array:8 [
    "Server" => array:1 [
      0 => "nginx/1.10.1 (Ubuntu)"
    ]
    "Content-Type" => array:1 [
      0 => "text/html; charset=UTF-8"
    ]
    "Transfer-Encoding" => array:1 [
      0 => "chunked"
    ]
    "Connection" => array:1 [
      0 => "keep-alive"
    ]
    "Cache-Control" => array:1 [
      0 => "no-cache"
    ]
    "X-RateLimit-Limit" => array:1 [
      0 => "60"
    ]
    "X-RateLimit-Remaining" => array:1 [
      0 => "55"
    ]
    "Date" => array:1 [
      0 => "Mon, 26 Dec 2016 07:11:05 GMT"
    ]
  ]
  -headerNames: array:8 [
    "server" => "Server"
    "content-type" => "Content-Type"
    "transfer-encoding" => "Transfer-Encoding"
    "connection" => "Connection"
    "cache-control" => "Cache-Control"
    "x-ratelimit-limit" => "X-RateLimit-Limit"
    "x-ratelimit-remaining" => "X-RateLimit-Remaining"
    "date" => "Date"
  ]
  -protocol: "1.1"
  -stream: Stream {#164
    -stream: stream resource @11
      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: []
  }
}
0 likes
2 replies
mhdd's avatar
mhdd
OP
Best Answer
Level 9

Omg i solved it. the problem is i cant get the data normaly. i should cast the guzzle response object to string. something like this

convert this :

Stream {#164 ▼
  -stream: :stream {@10 ▶}
  -size: null
  -seekable: true
  -readable: true
  -writable: true
  -uri: "php://temp"
  -customMetadata: []
}

to :

$response->getBody()->__toString();

Please or to participate in this conversation.