yes, but before returning the content also check if the response is successful. https://laravel.com/docs/8.x/http-client#making-requests
May 23, 2021
4
Level 3
3rd Party API Service Class
I want to call 3rd party API service and do some activities. So i prepared this Service class
Actually I want to know where i m in Right Track. or Do i need to follow up any other way to accomplish my task . Please let me know your suggestion.
<?php
namespace App\Services;
use GuzzleHttp\Client;
class VideoAPI
{
protected $token;
protected $apiUrl;
protected $headers;
protected $query;
public function __construct()
{
$token = config('app.token');
$apiUrl = config('app.apiUrl');
$headers = [
'Authorization' => 'Bearer ' . $token,
];
$query = [
'page' => 1,
'size' => 100,
'sort' => 'name:-1',
];
$this->apiUrl = $apiUrl;
$this->token = $token;
$this->headers= $headers;
$this->query = $query;
}
public function getProjects()
{
$client = new Client();
$client = $client->request('GET', $this->apiUrl, [
'headers' => $this->headers,
'query' => $this->query
]);
return $client->getBody()->getContents();
}
}
Please or to participate in this conversation.