Source: juststeveking-uk-astro.pages.dev

Working with third party services in Laravel
I have been working with 3rd party APIs for longer than I care to admit, over time I have found a few ways that work nicely for me. In this article, I am going to walk through how I connect and work with 3rd party APIs - what has worked for me and how I like to approach this task.

Now we just need to pull this trait into our Client class, and we can start faking requests: namespace App\Services\PingPing; use App\Services\Concerns\HasFake; class Client { use HasFake; public function __construct( protected string $uri, protected string $token, ) {}}

Lets refactor our Client class to implement these things: namespace App\Services\PingPing; use App\Services\Concerns\HasFake; use Illuminate\Support\Facades\Http; class Client { use HasFake; public function __construct( protected string $uri, protected string $token, protected int $timeout = 10, protected null|int $retryTimes = null, protected null|int $retryMilliseconds = null, ) {} public function monitors() { $request = Http::withToken( token: $this->token, )->withHeaders([ 'Accept' => 'application/json', ])->timeout( seconds: $this->timeout, ); if ( !

Let’s create our Monitor DTO under app/Services/PingPing/DTO/Monitor.php which should look like this: namespace App\Services\PingPing\DTO; use Spatie\DataTransferObject\DataTransferObject; class Monitor extends DataTransferObject { public int $id; public string $identifier; public string $alias; public string $scheme; public string $host; public string $port; public string $url; public string $statusPage; public Checks $checks; }
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Community Partners