src/Service/CallApiService.php line 98

  1. <?php
  2. namespace App\Service;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Contracts\HttpClient\HttpClientInterface;
  6. class CallApiService
  7. {
  8.     private $client;
  9.     public function __construct(HttpClientInterface $client)
  10.     {
  11.         $this->client $client;
  12.     }
  13.     public function callApi($page): ?array
  14.     {
  15.         $range "0-9";
  16.         if ($page != null) {
  17.             $max $page;
  18.             $min $max 9;
  19.             $range $min '-' $max;
  20.         }
  21.         $token $this->client->request(
  22.             'POST',
  23.             'https://entreprise.pole-emploi.fr/connexion/oauth2/access_token?realm=%2Fpartenaire',
  24.             [
  25.                 'headers' =>
  26.                 [
  27.                     'Content-Type' => 'application/x-www-form-urlencoded'
  28.                 ],
  29.                 'body' =>
  30.                 [
  31.                     'client_id' => 'PAR_salondelindustrie41_fe3f55099e765f46c092b25f44880cdf0c3ac624de2bf615b4dfd3e4d1e38a3d',
  32.                     'client_secret' => 'b62c2085f7dde17e8d10eda93729c870f66ec7d39a2ecd139a02917191d1390b',
  33.                     'grant_type' => 'client_credentials',
  34.                     'scope' => 'api_offresdemploiv2 o2dsoffre'
  35.                 ]
  36.             ]
  37.         )
  38.         // ->getContent()
  39.         ;
  40.         // $httpLogs = $token->getInfo('debug');
  41.         // dump(json_decode($token->getContent('access_token')));
  42.         // dump($httpLogs);
  43.         $contentjson_decode($token->getContent(), true);
  44.         $access_token $content['access_token'];
  45.         //dump($access_token);
  46.         $response $this->client->request(
  47.             'GET',
  48.             'https://api.pole-emploi.io/partenaire/offresdemploi/v2/offres/search',
  49.             [
  50.                 'headers' =>
  51.                 [
  52.                     'Authorization' => 'Bearer ' $access_token,
  53.                 ],
  54.                 'query' => [
  55.                     // Nb d'annonces
  56.                     'range' => $range,
  57.                     // Tri par mot-clé
  58.                     // 'motsCles' => '',
  59.                     // H -> Industrie
  60.                     // I -> Installation / Maintenance
  61.                     // N -> Transport / Logistique
  62.                     'grandDomaine' => 'H,I,N',
  63.                     'departement' => '41',
  64.                     // 0 -> Tri par pertinence décroissante, distance croissante, date de création décroissante
  65.                     // 1 -> Tri par date de création décroissant, pertinence décroissante, distance croissante
  66.                     // 2 -> Tri par distance croissante, pertinence décroissante, date de création décroissante
  67.                     'sort' => '1',
  68.                     // Tri par domaine
  69.                     // 'grandDomaine' => 'Commerce / Vente',
  70.                     // Tri par commune
  71.                     // 'commune' => '44109', // code INSEE de la commune
  72.                     //'commune' => '41018,41194,41269,41295,41136,41232,41242,41106,41047,41149,41059,41212,41151,41167,41226,41150,41198',
  73.                     // 'distance' => '0',
  74.                 ]
  75.             ]
  76.         );
  77.         $data json_decode($response->getContent(), true);
  78.         return $data;
  79.     }
  80.     public function searchKeyword($motcle,$page)
  81.     {
  82.         $range "0-9";
  83.         if ($page != null) {
  84.             $max $page;
  85.             $min $max 9;
  86.             $range $min '-' $max;
  87.         }
  88.         // dump($motcle);
  89.         $motcle str_replace('+'','$motcle);
  90.         // Effectuer la requête à l'API de Pôle emploi
  91.         $token $this->client->request(
  92.             'POST',
  93.             'https://entreprise.pole-emploi.fr/connexion/oauth2/access_token?realm=%2Fpartenaire',
  94.             [
  95.                 'headers' =>
  96.                 [
  97.                     'Content-Type' => 'application/x-www-form-urlencoded'
  98.                 ],
  99.                 'body' =>
  100.                 [
  101.                     'client_id' => 'PAR_salondelindustrie41_fe3f55099e765f46c092b25f44880cdf0c3ac624de2bf615b4dfd3e4d1e38a3d',
  102.                     'client_secret' => 'b62c2085f7dde17e8d10eda93729c870f66ec7d39a2ecd139a02917191d1390b',
  103.                     'grant_type' => 'client_credentials',
  104.                     'scope' => 'api_offresdemploiv2 o2dsoffre'
  105.                 ]
  106.             ]
  107.         );
  108.         $contentjson_decode($token->getContent(), true);
  109.         $access_token $content['access_token'];
  110.         $response $this->client->request(
  111.             'GET',
  112.             'https://api.pole-emploi.io/partenaire/offresdemploi/v2/offres/search',
  113.             [
  114.                 'headers' =>
  115.                 [
  116.                     'Authorization' => 'Bearer ' $access_token,
  117.                 ],
  118.                 'query' => [
  119.                     // Nb d'annonces sur la page
  120.                     'range' => $range,
  121.                     // H -> Industrie
  122.                     // I -> Installation / Maintenance
  123.                     // N -> Transport / Logistique
  124.                     'grandDomaine' => 'H,I,N',
  125.                     'departement' => '41',
  126.                     'motsCles' => $motcle
  127.                 ]
  128.             ]
  129.         );
  130.         // dump($response->getStatusCode());
  131.         $data json_decode($response->getContent(), true);
  132.         return $data;
  133.     }
  134. }