Thanks for the help. It's now working!
I'm updating my booking with a webhook PHP script. It automatically enters a door code in one of the individual fields 14 days before arrival.
The booking number is passed to the script via a URL parameter. I have not tested the alternative with file_get_contents('php://input');.
Code: Select all
<?php
/*
* Aufruf im Webhook der Autoaction mit:
* https://domain.de/tuercode-setzen.php?xb=[BOOKID]&xprop=[PROPERTYID]
*/
/* Parameter prüfen */
$bookId = filter_var($_GET["xb"], FILTER_SANITIZE_STRING);
$property = filter_var($_GET["xprop"], FILTER_SANITIZE_STRING); /* notwendig wg. passendem API Key */
$auth = array();
$auth['apiKey'] = 'apikey123456789';
$auth['propKey'] = '';
$propKey_fewo = 'keyprop1';
$propKey_chalet = 'keyprop2';
$path_doorcode_file = '../path1/codefile.csv'; /* relativ zum Script */
/* API Key nach Property auswählen */
if ($property == 123456 ) { $auth['propKey'] = $propKey_fewo;
} elseif ($property == 654321) { $auth['propKey'] = $propKey_chalet;
} else {
exit;
}
/* Türcode Datei in Array einlesen */
$tuercodes = array();
$tuercodes[] = array();
foreach (file($path_doorcode_file, FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES) as $row) {
$felder = explode(',', $row);
$tuercodes[$felder[0]][$felder[1]] = $felder[2];
}
/* Buchung über ID einlesen */
$data = array();
$data['authentication'] = $auth;
$data['bookId'] = $bookId;
$json = json_encode($data);
$url = "https://api.beds24.com/json/getBookings";
$ch=curl_init();
curl_setopt($ch, CURLOPT_POST, 1) ;
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = json_decode(curl_exec($ch), true);
curl_close ($ch);
/* Felder Anreise, Abreise, Tage aus der Buchung ermitteln */
$firstnight = date("d.m.Y", strtotime($result[0]["firstNight"]) );
$firstDate = new DateTime($result[0]["firstNight"]);
$secondDate = new DateTime($result[0]["lastNight"]);
$intvl = $firstDate->diff($secondDate);
$numdays = $intvl->days + 1; /* Tage = Anzahl Nächte + 1 */
/* Anzahl Tage für Zugriff Codetabelle ermitteln */
/* bei 1 Tag = 2 Tages Code, bei mehr als 7 Tagen = 14 Tage Tuercode */
$hf_numdays = $numdays;
if ( $numdays == 1 ) { $hf_numdays = 2; }
if ( $numdays > 7 ) { $hf_numdays = 14; }
/* Buchung mit neuen Werten schreiben */
$data = array();
$data['authentication'] = $auth;
if ( empty($tuercodes[$firstnight][$hf_numdays])) {
$data['custom8'] = "*** Fehler Tuercode ***";
}
else
{
$data['custom8'] = $tuercodes[$firstnight][$hf_numdays]; /* neuer Tuercode */
}
$data['custom8'] = str_pad($data['custom8'], 6, 0, STR_PAD_LEFT); /* 6-stellig mit führenden 0 */
if ( $numdays > 14 ) { $data['custom8'] = $data['custom8']. " *** gilt nur 14 Tage ***" ; }
$data['bookId'] = $bookId;
/*
Testausgabe in Feld Nachricht:
$test = '';
foreach (getallheaders() as $name => $value) { $test = $test . "$name: $value\n"; }
$test = $test . $data['bookId'] . "\n";
$test = $test . $data['custom8'] . "\n";
$test = $test . $result[0]["lastNight"] . "\n";
$data['message'] = $test;
*/
/* API Aufruf: Update der Buchung mit JSON setBooking */
$json = json_encode($data);
$url = "https://api.beds24.com/json/setBooking";
$ch=curl_init();
curl_setopt($ch, CURLOPT_POST, 1) ;
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
curl_close ($ch);
echo $result;
?>
btw: The blue background for code is very distracting. It would be great to change the forum setting here.