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

jmacdiarmid's avatar

Building a wrapper? for an external api

I'm using Laravel 8 and need to integrate a third-party "external" api into my app but I'm not sure how to go about it. I've successfully connected to the third-party service using Guzzle, made my first "get" and I received a json string as expected but not sure where to go from there. I've read some articles that say I need to use an APIresource to transform the data into a format that can be saved to a database and/or rendered in a view.

I found a post here on Laracasts https://laracasts.com/discuss/channels/general-discussion/create-api-wrapper-best-practice and started to go through the course here https://laracasts.com/series/incremental-api-development. Both sources are 5 years old.

I've walked through several articles on API development that suggest creating a custom facade, custom service provider, etc. and tried to duplicate what they the poster demonstrated but can't seem to get the code to work; in the router, the alias of the facade isn't found and wants me to create the class in the route file.

Most examples I find are using Laravel version prior to Laravel 8.

Articles: https://www.codecheef.org/article/how-to-create-and-use-custom-facade-in-laravel-6 https://medium.com/@cmanish049/creating-custom-facades-in-laravel-b9b72d573752

The api I need to integrate is for the Simpli.fi advertising for digital marketing agencies and media companies. https://simpli.fi/

My goal is to find an updated resource that will show me how to do this and I'm hoping someone can point me in the right direction. The Laravel documentation is a bit vague so another "hope" I have is that I can find an example.

0 likes
6 replies
martinbean's avatar
Level 80

@jmacdiarmid API resources are for returning responses from your application. They‘re essentially view presenters for your application’s models to convert them to a JSON response.

In your case, instantiating a Guzzle client and making a GET request is the basis for interacting with most external APIs. So you could move that code to a “service” class for that particular API. Don’t be too concerned about the naming; a service class is essentially just a plain ol’ PHP class that contains your logic.

So, if you were working with an API for a fictitious service called “Foo”, then you could have a “FooService” class that looks like this:

namespace App\Services;

use GuzzleHttp\Client;

class FooService
{
    protected $client;

    public function __construct(Client $client)
    {
        $this->client = $client;
    }

    public function getBars()
    {
        $response = $this->client->get('https://example.com/bars');

        return json_decode($response->getBody());
    }
}

You could inject this class in your controller, Artisan command, queue job or whatever, and then call its methods to get the data from the API:

class SomeController extends Controller
{
    protected $foo;

    public function __construct(FooService $foo) 
    {
        $this->foo = $foo;
    }

    public function someAction()
    {
        $bars = $this->foo->getBars();
    }
}

And that’s pretty much it! You now have a service class, whose only responsibility is for interacting with the API, meaning if the API changes, then your changes are constrained to that class only—all of your other consuming classes (such as the controller in the above example) won’t need any changes as the interaction with the API is constrained to the FooService only.

So long as your getBars method keeps returning the same “shape” response, then you don’t need to update any code that consumes that class and its methods.

5 likes
jlrdw's avatar

Also @jmacdiarmid when you mentioned API development, that is when you are creating an api for someone else to use.

You just have to decide what the data is for. Are you just displaying, or you saving it.

If not saving then just deal with a view to display it.

I usually like using an array if I need to loop over such data, so I json_decode.

An example: https://gist.github.com/jimgwhit/cbbe5bb0d2556fdc7e37a86d3630239c But that's just my preference.

Of course you can use a collection as well.

jmacdiarmid's avatar

I will need to capture data for new client campaigns using a form and will need to post the data using the simplifi api. Once the campaign data has been submitted then we plan to use the api for tracking and reporting purposes. Any api used for this app will be third-party apis for instance, I'm also planning to use the Asana api to automate the creation of new tasks when a contract is approved by our client.

jlrdw's avatar

So you are interacting in both directions sending data and some instances iretrieving data.

I have done similar with adopt a pet. But the API you are interacting with should have a good set of instructions for usage, both retrieving and uploading data.

If they lack proper instructions, find yourself a video on YouTube or somewhere with a similar API that shows good examples.

First time doing this is tricky.

1 like
jmacdiarmid's avatar

Indeed, it is tricky. I've been researching this off and on while over the last 4-5 months. Most of the examples on the api docs are using curl cli. I found a site with quite a few conversion tools including a tool that will convert curl cli commands to php. https://onlinedevtools.in/curl

What's also surprising is that Simpli.fi doesn't have any examples on GitHub. I did manage to stumble across a script that shows how send a "GET" query which was written in python.

The only thing I've found so far on YT is tips on advertising using their service such as Retargeting, Geo-targeting and Geofencing.

Please or to participate in this conversation.