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

oten's avatar
Level 1

Best approach to handle third party API responses

What would be the method of choice for handling responses coming from a REST API?

For example consider the following response:

[
  "name": "John Doe",
  "username": "john",
  "posts": [
     0: => [
       "title" => "First Post",
       "body" => "This is the first post."
     ]
     1: => [
       "title" => "Second Post",
       "body" => "This is the second post."
     ]
  ]
]

Basically what I'm trying to do is to have a separate object for each post returned by the API. I assume that one way would be to create two classes (not Eloquent models since I don't want the response to be stored in my database) "User" and "Post" and loop through the array to create objects accordingly.

According to this article I could use Laravel Resources to consume the response but that doesn't seem to work in my case.

0 likes
2 replies
ahmeddabak's avatar

I would create an Adapter Class, which take the response and handle it to fit my project, like extract data from the response to properties and the adapter can also expose some helpful methods when needed

shez1983's avatar

i agree. you would want to create an adapter or wrapper so every call to the 3rd party is wrapped with how you want the data to be displayed.. also you can then add caching on this wrapper..

Please or to participate in this conversation.