Advertisement
moonshine30

SNCF CRON V2

Jul 17th, 2019
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. <?php
  2. // API Interract
  3. function getTrains($api_sncf_user, $api_sncf_mdp, $VilleDepart, $VilleArrivee, $date, $NbJourneys){
  4.  
  5.     try {
  6.         $url= "https://".$api_sncf_user."@api.sncf.com/v1/coverage/sncf/journeys?from=".$VilleDepart."&to=".$VilleArrivee."&datetime=".$date."&datetime_represents=departure&min_nb_journeys=".$NbJourneys."";
  7.  
  8. //Try file get contents
  9.         $data = file_get_contents($url);
  10.         json_decode($data, true);
  11.  
  12. //Try Curl
  13.         $ch = curl_init();
  14.         curl_setopt($ch, CURLOPT_URL, $url);
  15.         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
  16.         curl_setopt($ch, CURLOPT_HEADER, 1);
  17.         curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  18.         curl_setopt($ch, CURLOPT_VERBOSE, true);
  19.         $response = curl_exec($ch);
  20.         $resultData = json_decode($response, true);
  21.  
  22. //DBUG//
  23.         echo 'JSON FGC: ';
  24.         var_dump($data);
  25.         echo '<br/>';
  26.         echo 'JSON CURL: ';
  27.         var_dump($resultData);
  28.         echo '<br/>';
  29.         echo 'API URL : '.$url.'<br/>';
  30.         echo 'API USER : '.$api_sncf_user.'<br/>';
  31.         echo 'API MDP : '.$api_sncf_mdp.'<br/>';
  32.         echo 'API $VilleDepart : '.$VilleDepart.'<br/>';
  33.         echo 'API $VilleArrivee : '.$VilleArrivee.'<br/>';
  34.         echo 'API $date : '.$date.'<br/>';
  35.         echo 'API $NbJourneys : '.$NbJourneys.'<br/>';
  36.  
  37.     } catch (Exception $e) {
  38.         echo 'Exception reçue : ',  $e->getMessage(), "\n";
  39.     }
  40.  
  41. }
  42.  
  43. // Identifiants de l'API SNCF https://data.sncf.com/api/fr/documentation
  44. $api_sncf_user = 'd8c032e8-695f-46be-b6cf-f57580ad4ca2';
  45. $api_sncf_mdp  = '';
  46.  
  47. // Initialiation des variables
  48. // Pour trouver l'id de la ville rechercher "id" dans la requete suivante https://api.sncf.com/v1/coverage/sncf/places?q=LYON
  49. $VilleDepart  = 'admin:fr:63052'; // Le Breuil sur Couze ( Marie stop area)
  50. $VilleArrivee = 'admin:fr:63113'; // Clermont-ferrand (la pardieu stop area  stop_area:OCE:SA:87782607 )
  51. $date = "20190714";
  52. $NbJourneys ="4"; //Nb de trains minimum par jour à afficher
  53.  
  54. getTrains($api_sncf_user, $api_sncf_mdp, $VilleDepart, $VilleArrivee,$date,$NbJourneys);
  55.  
  56.  
  57. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement