2

I'm building an application in Laravel 5.5 where I'm calling an api request to get response, I've following in my controller:

$client = new GuzzleHttp\Client();
$response = $client->request('GET', 'http://www.conxn.co.in/CoxnsvcA.svc/Login');
dd($response);

I'm able to get something like this:

Test

But while doing $response->getBody(); I'm unbale to get the response. when I do dd($response->getBody() I'm getting:

enter image description here

In postman it is showing something like this:

test in postman

Help me out with this.

Thanks.

8
  • 2
    Have you tried (string) $response->getBody()? The method will return a stream, which requires special handling depending on what you are trying to do. Maybe you can show a larger part of the code? For example what do you get get instead of the response data when you call $response->getBody()? Commented Dec 15, 2017 at 10:16
  • @dbrumann It is showing stream sets but I can't find the response where I can retrieve. Give me 2 mins I'll update the question with output value. Commented Dec 15, 2017 at 10:19
  • Seems there is an error. In postman result show there is an error with parameter. Commented Dec 15, 2017 at 10:22
  • @UlrichDohou yes some update was happening, please check now. Commented Dec 15, 2017 at 10:26
  • 2
    As I said, the body is a stream. When you do (string) $response->getBody() it will make it a regular string. You could also use Guzzle's helper method $data = $response->json(); to get the data from the response already parsed, when you know it's json Commented Dec 15, 2017 at 10:29

2 Answers 2

4

This should work:

$client = new GuzzleHttp\Client();
$response = $client->request('GET','http://www.conxn.co.in/CoxnsvcA.svc/Login');

$response_body = json_decode($response->getBody());
Sign up to request clarification or add additional context in comments.

Comments

1

This should work for you.

$client = new GuzzleHttp\Client();

$response = $client->request("GET", "http://www.googleapi.com", ['headers' => ['Accept' => 'application/json']]); 

$response_data = json_decode((string) $response->getBody(), true);

return collect($response_data);

1 Comment

Helps somehow with that json_decode((string) + true. Good one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.