My first API script - beginner's questions

Develper issues when using the Beds24 APi.
Post Reply
scheuermatthof.de
Posts: 12
Joined: Fri Sep 17, 2021 1:13 pm

I would like to set a door code in my bookings via the API. As a beginner, there are a few basic hurdles right at the beginning:
  1. How can I debug the API call in a meaningful way? At the moment, I write individual variables in the note of the booking. Is there a better way?
  2. How do I transfer data from the booking to my API script? I thought this was passed via the HTTP header. But this does not work. Does anyone have an example?
The script is called via an autoaction per webhook. The call itself works. But I can't get variables to be passed. I have entered "individual header" in the autoaction:

zz_bookid:[BOOKID]
zz_anreise:[FIRSTNIGHT:{%A, %e %B, %Y}]
zz_tage:[NUMNIGHT]

Here my example php script:

Code: Select all

<?php

$auth = array();
$auth['apiKey'] = 'xxxxxx';
$auth['propKey'] = 'xxxxxxxxx';

$data = array();
$data['authentication'] = $auth;

/* Workload later here */
$data['bookId'] = '30042266';
$data['custom8'] = 'egal123456';   /* neuer Tuercode */ 

/* TEST */
$test = '';
foreach (getallheaders() as $name => $value) {
    $test = $test . "$name: $value\n";
}
$data['notes'] = $test;   /* neuer Tuercode */ 

/*
$test = "zz_bookid:" . filter_var($_GET["zz_bookid"], FILTER_SANITIZE_STRING) . '\n';
$test = $test . "zz_anreise" . filter_var($_GET["zz_anreise"], FILTER_SANITIZE_STRING) . '\n';
$test = $test . "zz_tage:" .  filter_var($_GET["zz_tage"], FILTER_SANITIZE_STRING) . '\n';
$test = $test . "QUERY_STRING:" . $_SERVER['QUERY_STRING']; */

/* eigentlicher API Aufruf */
$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;	
?>
scheuermatthof.de
Posts: 12
Joined: Fri Sep 17, 2021 1:13 pm

I'm still looking for help:

How can I connect a booking/autoaction via WEBHOOK to a PHP-Script?

Here are my values withn in the webhook autoaction:
URL: https://domainname.de/tuercode-setzen.php
Individueller Header: zz_bookid:[BOOKID]
zz_anreise:[FIRSTNIGHT:{%A, %e %B, %Y}]
zz_tage:[NUMNIGHT]
How can I know and access the booking-id within my PHP-Script?
markkinchin
Site Admin
Posts: 935
Joined: Fri Mar 02, 2012 1:43 pm

You would be better to put the data you want to send to your system in the "Body Data" section of the webhook if you can.
PHP on your system can read the body data with a command like file_get_contents('php://input');

You can process the body data extracting what you need and if you need more information make a call back to the beds24 API to read the entire booking from JSON getBookings specifying the booking Id you got from the webhook.
scheuermatthof.de
Posts: 12
Joined: Fri Sep 17, 2021 1:13 pm

Thank you very much. I will try this way next. I don't unterstand why I don't get the informations also within the header?

In the meantime, I was also able to submit the booking ID once directly via the URL, in the form:
https://domain.de/files/webcam/tuercode ... id=[BOOKID]

Unfortunately, the manual autoaction no longer works in my test case after some time without any reason and I get nowhere. I have now opened a support ticket for this.
scheuermatthof.de
Posts: 12
Joined: Fri Sep 17, 2021 1:13 pm

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.
Post Reply