- Création API recherche
This commit is contained in:
25
src/Controller/APITestController.php
Normal file
25
src/Controller/APITestController.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Service\GoogleBooksService;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
class APITestController extends AbstractController
|
||||
{
|
||||
private GoogleBooksService $googleBooksService;
|
||||
|
||||
public function __construct(GoogleBooksService $googleBooksService)
|
||||
{
|
||||
$this->googleBooksService = $googleBooksService;
|
||||
}
|
||||
|
||||
#[Route('/api/search', name: 'api_search')]
|
||||
public function search(): JsonResponse
|
||||
{
|
||||
$results = $this->googleBooksService->searchBooks('Le loup des steppes');
|
||||
return $this->json($results);
|
||||
}
|
||||
}
|
31
src/services/GoogleService.php
Normal file
31
src/services/GoogleService.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
class GoogleBooksService
|
||||
{
|
||||
private HttpClientInterface $client;
|
||||
|
||||
public function __construct(HttpClientInterface $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function searchBooks(string $query, string $lang = 'fr'): array
|
||||
{
|
||||
$url = 'https://www.googleapis.com/books/v1/volumes';
|
||||
$response = $this->client->request('GET', $url, [
|
||||
'query' => [
|
||||
'q' => $query,
|
||||
'langRestrict' => $lang,
|
||||
],
|
||||
]);
|
||||
|
||||
// Convertir la réponse JSON en tableau PHP
|
||||
$dataArray = $response->toArray();
|
||||
dump($dataArray);
|
||||
return $dataArray;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user