Last active
August 29, 2015 14:21
-
-
Save mmuman/5ed425a130e65224b184 to your computer and use it in GitHub Desktop.
generates urls to voyages-sncf.com to book trains with prefilled form items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* voyages-sncf.php - generates urls to voyages-sncf.com with prefilled form items. | |
* | |
* Copyright 2011, Francois Revol, [email protected]. | |
* Distributed under the terms of the MIT License. | |
*/ | |
/* | |
* currently those urls look like: | |
http://www.voyages-sncf.com/billet-train?_LANG=FR&_AGENCY=VSC&COMFORT_SMOKING=N&DISTRIBUTED_COUNTRY=FR&SOURCE=EXPR_BOOK_HP&ORIGIN_CITY=&DESTINATION_CITY=TAIN%20GARE%20SNCF%20%2826%29&OUTWARD_DATE=11/11/2011&OUTWARD_TIME=07&INWARD_DATE=13/11/2011&INWARD_TIME=7&COMFORT_CLASS=2&NB_TYPO_ADULT=1&DIRECT_TRAVEL_CHECK=off&FLEXIBLE=off&PASSENGER_1=ADULT&PASSENGER_1_CARD=default&PASSENGER_1_FID_PROG=default&PASSENGER_1FID_NUM=&PASSENGER_1_FAM_PERCENTAGE=30&PASSENGER_2=ADULT&PASSENGER_2_CARD=default&PASSENGER_2_FID_PROG=default&PASSENGER_2FID_NUM=&PASSENGER_2_FAM_PERCENTAGE=30&PASSENGER_3=ADULT&PASSENGER_3_CARD=default&PASSENGER_3_FID_PROG=default&PASSENGER_3FID_NUM=&PASSENGER_3_FAM_PERCENTAGE=30&PASSENGER_4=ADULT&PASSENGER_4_CARD=default&PASSENGER_4_FID_PROG=default&PASSENGER_4FID_NUM=&PASSENGER_4_FAM_PERCENTAGE=30&PASSENGER_5=ADULT&PASSENGER_5_CARD=default&PASSENGER_5_FID_PROG=default&PASSENGER_5FID_NUM=&PASSENGER_5_FAM_PERCENTAGE=30&PASSENGER_6=ADULT&PASSENGER_6_CARD=default&PASSENGER_6_FID_PROG=default&PASSENGER_6FID_NUM=&PASSENGER_6_FAM_PERCENTAGE=30 | |
* arguments look like: | |
_LANG=FR | |
_AGENCY=VSC | |
COMFORT_SMOKING=N | |
DISTRIBUTED_COUNTRY=FR | |
SOURCE=EXPR_BOOK_HP | |
ORIGIN_CITY= | |
DESTINATION_CITY=TAIN%20GARE%20SNCF%20%2826%29 | |
OUTWARD_DATE=13/02/2011 | |
OUTWARD_TIME=20 | |
INWARD_DATE= | |
INWARD_TIME=7 | |
COMFORT_CLASS=2 | |
NB_TYPO_ADULT=1 | |
DIRECT_TRAVEL_CHECK=off | |
FLEXIBLE=off | |
PASSENGER_1=ADULT | |
PASSENGER_1_CARD=default | |
PASSENGER_1_FID_PROG=default | |
PASSENGER_1FID_NUM= | |
PASSENGER_1_FAM_PERCENTAGE=30 | |
... | |
PASSENGER_6=ADULT | |
PASSENGER_6_CARD=default | |
PASSENGER_6_FID_PROG=default | |
PASSENGER_6FID_NUM= | |
PASSENGER_6_FAM_PERCENTAGE=30 | |
*/ | |
/** | |
* This function generates an url to the voyages-sncf.com website to prefill the train reservation form. | |
* Cette fonction génère une url vers le site web voyages-sncf.com pour pré-remplir le formulaire de réservation de train. | |
* @author François Revol | |
* @param from Nom de la gare de départ | |
* @param outward Date de départ (NULL ou une chaîne au format DATE_ATOM (exemple : 2005-08-15T15:52:01+00:00) ou un objet DateTime | |
* @param inward Date de retour (NULL ou une chaîne au format DATE_ATOM (exemple : 2005-08-15T15:52:01+00:00) ou un objet DateTime | |
* @param passengers Nombre de passagers (1 par défaut) | |
* @param comfort Classe de confort (1 ou 2, 2 par défaut) | |
* @param lang Code ISO de la langue à utiliser pour le formulaire (doit rester être 'FR' !) | |
* @return l'url pré-encodée vers le formulaire pré-rempli | |
*/ | |
function linkto_voyages_sncf_com($from, $to, $outward=NULL, $inward=NULL, $passengers = 1, $comfort = 2, $lang='FR') | |
{ | |
$baseurl='http://www.voyages-sncf.com/billet-train?'; | |
// default departure hours | |
$outward_h = "7"; | |
$inward_h = "7"; | |
// AS = 1 way, AR = round-trip | |
$travel_type = "AS"; | |
if (!isset($outward)) { | |
$date = time(); | |
} else if (is_string($outward)) { | |
$date = @strtotime($outward); | |
} else if (is_numeric($outward)) { | |
$date = $outward; | |
} else | |
$date = time(); | |
$outward = @date("d/m/Y", $date); | |
$outward_h = @date("G", $date); | |
if (!isset($inward) || empty($inward)) { | |
$date = NULL; | |
} else if (is_string($inward)) { | |
$date = @strtotime($inward); | |
} else if (is_numeric($inward)) { | |
$date = $inward; | |
} else | |
$date = NULL; | |
if (isset($date)) { | |
$travel_type = "AR"; | |
$inward = @date("d/m/Y", $date); | |
$inward_h = @date("G", $date); | |
} else | |
$inward = ""; | |
/* Only works with PHP > 5.2.0 | |
if (!isset($outward)) { | |
$date = new DateTime("now", new DateTimeZone("Europe/Paris")); | |
$outward = $date->format("d/m/Y"); | |
} else if (is_object($outward)) { | |
$outward_h = $outward->format("G"); | |
$outward = $outward->format("d/m/Y"); | |
} else if (is_string($outward)) { | |
$date = new DateTime($outward, new DateTimeZone("Europe/Paris")); | |
$outward_h = $date->format("G"); | |
$outward = $date->format("d/m/Y"); | |
} | |
if (!isset($inward)) { | |
$inward = ""; | |
} else if (is_object($inward)) { | |
$travel_type = "AR"; | |
$inward_h = $inward->format("G"); | |
$inward = $inward->format("d/m/Y"); | |
} else if (is_string($inward)) { | |
$date = new DateTime($inward, new DateTimeZone("Europe/Paris")); | |
$travel_type = "AR"; | |
$inward_h = $date->format("G"); | |
$inward = $date->format("d/m/Y"); | |
} | |
*/ | |
$arguments = array( | |
"_LANG" => strtoupper($lang), | |
"_AGENCY" => "VSC", | |
"TRAVEL_TYPE" => $travel_type, | |
"COMFORT_SMOKING" => "N", | |
"DISTRIBUTED_COUNTRY" => "FR", | |
"SOURCE" => "EXPR_BOOK_HP", | |
"ORIGIN_CITY" => $from, | |
"DESTINATION_CITY" => $to, | |
"OUTWARD_DATE" => $outward, | |
"OUTWARD_TIME" => $outward_h, | |
"INWARD_DATE" => $inward, | |
"INWARD_TIME" => $inward_h, | |
"COMFORT_CLASS" => $comfort, | |
"NB_TYPO_ADULT" => $passengers, | |
"DIRECT_TRAVEL_CHECK" => "off", | |
"FLEXIBLE" => "off" | |
); | |
return $baseurl . http_build_query($arguments); | |
} | |
/* testing... | |
function ahref($link, $name=NULL) { | |
return '<a href="' . $link . '">' . (isset($name) ? $name : $link) . '</a>'; | |
} | |
echo '<a href="' . linkto_voyages_sncf_com('', 'TAIN GARE SNCF (26)', '2011-11-11T06:52:01', '2011-11-13T18:52:01') . '">Réservez votre train</a>'; | |
echo '<br>'; | |
print ahref(linkto_voyages_sncf_com('', 'TAIN GARE SNCF (26)')); | |
print '<br/><br/><br/>'; | |
print ahref(linkto_voyages_sncf_com('', 'TAIN GARE SNCF (26)', '2011-11-30T06:00:00Z')); | |
print '<br/><br/><br/>'; | |
print ahref(linkto_voyages_sncf_com('VALENCE', 'TAIN GARE SNCF (26)', '2011-11-30T06:00:00Z', '2011-12-03T16:00:00Z')); | |
print '<br/><br/><br/>'; | |
//print ahref(linkto_voyages_sncf_com('', 'TAIN GARE SNCF (26)', new DateTime('2011-11-11T06:52:01+00:00', new DateTimeZone("Europe/Paris")), new DateTime('2011-11-13T18:52:01+00:00', new DateTimeZone("Europe/Paris")))); | |
print '<br/><br/><br/>'; | |
print ahref(linkto_voyages_sncf_com('', 'TAIN GARE SNCF (26)', '2011-11-11T07:00:00', '2011-11-13T18:52:01+00:00')); | |
print '<br/><br/><br/>'; | |
print ahref(linkto_voyages_sncf_com('', 'BEAUVAIS', '2015-07-04T07:00:00', '2015-07-11T18:00:00+00:00')); | |
print '<br/><br/><br/>'; | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment