0

I call an API and the response is like this:

HTTP/1.1 201 Created 
Date: Tue, 12 Jun 2018 13:13:34 GMT
Server: Apache/2.4.x (Ubuntu)
Set-Cookie: PHPSESSID=id; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Length: 114
Connection: close
Content-Type: application/json

{"id":"id_code|id_code|id_code","error":{"code":0,"message":"message"}}

What I want to do is take only the json part from final:

{"id":"id_code|id_code|id_code","error":{"code":0,"message":"message"}}

Can I do this using PHP?

Thank you!

5
  • could you show ur calling part code? Commented Jun 12, 2018 at 14:40
  • Are you useing gazzele?? Commented Jun 12, 2018 at 14:41
  • @Davit you mean Guzzle? Commented Jun 12, 2018 at 14:41
  • http://docs.guzzlephp.org/en/stable/ lhis package?? Commented Jun 12, 2018 at 14:42
  • 1
    $response->getBody()->getContents() Commented Jun 12, 2018 at 14:45

1 Answer 1

1

You can get the contents of the response with $response->getBody()->getContent(), or you can cast the body to a string. From there if it is in JSON format you can decode it as normal:

// this works
$jsonResults = json_decode($response->getBody()->getContent(), true);

// so does this
$jsonResults = json_decode((string) $response->getBody(), true);
Sign up to request clarification or add additional context in comments.

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

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.