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

Randy_91's avatar

What is the best way to consume a REST API in Laravel 5.8

I am quite new to Laravel and need to consume a REST API and display the data on a page. Is there a specific modern way of doing this in 5.8 such as resources or is it just a case of using guzzle?

Should I create a service provider or some other class for this etc.

Can anyone point me at a tutorial

0 likes
3 replies
Snapey's avatar

Yes, probably put your REST API consumer code in its own class. Pass an instance of this class into your controller. It could be called a service provider but that requires you implement certain methods so that Laravel will bootstrap it. If its only used in one place then just create a simple class.

create a function in there which fetches and returns the data to the controller

controller creates a view and passes it the data.

Resources are more for manipulating data output as json so that you are giving the client just the data it needs and in the format it wants.

Randy_91's avatar

So far I have come up with this:

 use GuzzleHttp\Client;

  class RestAPI
  {
    protected $url;
    Protected $accessToken;
    protected $client;
    protected $headers;

public function __construct(Client $client)
{
    $this->url = 'https://gorest.co.in/public-api/';
    $this->accessToken = env('GOREST_ACCESS_TOKEN');
    $this->client= $client;
    $this->headers = [
        'cache-control' => 'no-cache',
        'content-type' => 'application/x-www-form-urlencoded',
        'Authorization' => 'Bearer ' . $this->accessToken,
    ];
}

public function getRequest(string $uri = null,  array $query = [])
{
    $full_path = $this->url;
    $full_path .= $uri

    $request = $this->client->get($full_path, [
        'headers'         => $this->headers,
        'timeout'         => 30,
        'connect_timeout' => true,
        'http_errors'     => true,
        'query' => $query
    ]);

    $response = $request ? $request->getBody()->getContents() : null;
    $status = $request ? $request->getStatusCode() : 500;

    if ($response && $status === 200 && $response !== 'null') {
        return (object) json_decode($response);
    }

    return null;
}

public function postRequest(string $uri = null, array $post_params = [])
{
    $full_path = $this->url;
    $full_path .= $uri;

    $request = $this->client>post($full_path, [
        'headers'         => $this->headers,
        'timeout'         => 30,
        'connect_timeout' => true,
        'http_errors'     => true,
        'form_params'     => $post_params,
    ]);

    $response = $request ? $request->getBody()->getContents() : null;
    $status = $request ? $request->getStatusCode() : 500;

    if ($response && $status === 200 && $response !== 'null') {
        return (object) json_decode($response);
    }

    return null;
}

Ok how is this looking? Could it be done better?

Please or to participate in this conversation.