User Tools

Site Tools


booking:examples

Examples

Requirements

To run the sample code you need PHP5 with the following modules:

Availability checking

The following code sample checks the availability of the offer, and it displays the price and possibly additional information from the organizer.

You need to count the age of passengers with an instant online quote and you are asked to send the form field for passengers, which will be filled with code, gender, and age.

$form_arr = array(
  'Person'=> array(
    array(
      'gender'=>'H', // adult
      'birthdate'=>'25.12.1980'
    ),
    array(
      'gender'=>'K', // child
      'birthdate'=>'25.12.2005'
    )
  )
);
$rqxml = $mdsws->genXMLRQ($auth, 'check', $params, $form_arr);
mdsws_check_example.php
<?php
/**
 * An example to check availability
 */
header('Content-Type: text/plain; charset=utf-8');
 
require_once 'mdswsBooking.class.php';
 
// login credentials
$auth = array(
    'login'=>'login',
    'pass'=>'password'
  );
// parameters to check availability
$params = array(
    'ofr_tourOp'=>'ITAK',
    'ofr_id'=>'3b9e9af3bea2b7e822df6805066de0cbc501a337f843d85689f84b47ac86e1f2',
    'par_adt'=>'2'
  );
 
$mdsws = new mdswsBookings();
$rqxml = $mdsws->genXMLRQ($auth, 'check', $params);
 
$check_res_xml = $mdsws->sendXML($rqxml);
$check_res = simplexml_load_string($check_res_xml);
 
if (isset($check_res->offerstatus) && isset($check_res->offerstatus->attributes()->status) && $check_res->offerstatus->attributes()->status == 'BA')
{
  echo "Oferta dostępna! \n";
  echo "Cena całkowita : ".$check_res->pricetotal->attributes()->price.' '.$check_res->pricetotal->attributes()->curr."\n";
  echo "Cena za os. : ".$check_res->priceperson->attributes()->price.' '.$check_res->priceperson->attributes()->curr."\n";
  if (isset($check_res->hints))
  {
    echo "Info od organizatora: \n\n";
    foreach ($check_res->hints->children() as $hint)
      echo $hint."\n";
  }
}
else
  echo 'Oferta niedostępna!';
 
// DEBUG
//echo "\n\n".print_r($check_res,true);
 
?>

Reservation

The following code displays the form (created with the data obtained after checking availability) which, after completion data for passengers, you need to use in action book to create a permanent or optional reservation.

mdsws_book_example.php
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Przykład rezerwacji oferty</title>
    <style type="text/css">
    /* <![CDATA[ */
    label { display:block; clear:both; }
    div { border:1px solid #888888; }
    div label { display:block; float:left; clear:none; }
    .personlabel { clear:both; }
    .error { margin:2px; border:2px solid #ff0000; }
    /* ]]> */
    </style>
  </head>
  <body>
<?php
/**
 * Example of book deals
 */
 
require_once 'mdswsBooking.class.php';
 
function renderFormField($fieldname, $fieldvalue)
{
  $ret = '';
  switch ($fieldvalue['type'])
  {
    case 'text':
      $ret .= '<input name="'.$fieldname.'" type="text" value="'.$fieldvalue['value'].'" />';
      break;
    case 'checkbox':
      $ret .= '<input name="'.$fieldname.'" type="checkbox" value="'.$fieldvalue['value'].'" '.($fieldvalue['checked']?'checked="checked"':'').' />';
      break;
    case 'select':
      $ret .= '<select name="'.$fieldname.'">';
      $ret .= '<option value=""></option>';
      for ($i=0; $i<count($fieldvalue['values']); $i++)
        $ret .= '<option value="'.$fieldvalue['values'][$i].'"'.($fieldvalue['values'][$i] == $fieldvalue['selected']?'selected':'').'>'.$fieldvalue['desc'][$i].'</option>';
      $ret .= '</select>';
      break;
    case 'hidden':
      $ret .= '<input name="'.$fieldname.'" type="hidden" value="'.$fieldvalue['value'].'" />';
      break;
    default:
      break;
  }
  return $ret;
}
 
// login credentials
$auth = array(
    'login'=>'login',
    'pass'=>'password'
  );
// parameters to check availability
$params = array(
    'ofr_tourOp'=>'ITAK',
    'ofr_id'=>'3b9e9af3bea2b7e822df6805066de0cbc501a337f843d85689f84b47ac86e1f2',
    'par_adt'=>'2'
  );
 
$mdsws = new mdswsBookings();
if (isset($_POST['Person']))
  $rqxml = $mdsws->genXMLRQ($auth, 'book', $params, $_POST);
else
  $rqxml = $mdsws->genXMLRQ($auth, 'check', $params);
 
$check_res_xml = $mdsws->sendXML($rqxml);
$check_res = simplexml_load_string($check_res_xml);
 
if (isset($check_res->offerstatus) && isset($check_res->offerstatus->attributes()->status) && $check_res->offerstatus->attributes()->status == 'BA')
{
  echo "<pre>";
  if (isset($check_res->booking_number))
    echo "Numer rezerwacji u operatora: ".$check_res->booking_number."\n";
  echo "</pre>";
  if (isset($check_res->booking_errors))
  {
    foreach ($check_res->booking_errors->children() as $error)
      echo '<div class="error">'.$error.'</div>';
  }
  echo "<pre>";
  echo "Oferta dostępna! \n";
  echo "Cena całkowita : ".$check_res->pricetotal->attributes()->price.' '.$check_res->pricetotal->attributes()->curr."\n";
  echo "Cena za os. : ".$check_res->priceperson->attributes()->price.' '.$check_res->priceperson->attributes()->curr."\n";
  if (isset($check_res->hints))
  {
    echo "Info od organizatora: \n\n";
    foreach ($check_res->hints->children() as $hint)
      echo $hint."\n";
  }
  echo "</pre>";
  if (isset($check_res->forminfo))
  {
    echo "Formularz: \n";
    echo '<form action="" method="POST">';
    $formArr = $mdsws->processFormData($check_res->forminfo);
 
    foreach ($formArr as $fieldname=>$fieldvalue)
    {
      if (!in_array($fieldname, array('Person','Client','add_service')))
      {
        echo '<label class="formfield">'.$fieldname.':&nbsp;';
        echo renderFormField($fieldname, $fieldvalue);
        echo '</label>';
      }
      elseif ($fieldname == 'Person')
      {
        foreach ($fieldvalue as $pkey=>$pvalue)
        {
          echo '<div><div class="personlabel">Osoba '.($pkey+1).':</div>';
          foreach ($pvalue as $pfieldkey=>$pfieldvalue)
          {
            echo '<label class="formfield">'.$pfieldkey.':&nbsp;';
            echo renderFormField($fieldname.'['.$pkey.']['.$pfieldkey.']', $pfieldvalue);
            echo '</label>';
          }
          echo '<div style="clear:both;"></div></div>';
        }
      }
      elseif ($fieldname == 'Client')
      {
        foreach ($fieldvalue as $cfieldkey=>$cfieldvalue)
        {
          echo '<label class="formfield">'.$cfieldkey.':&nbsp;';
          echo renderFormField($fieldname.'['.$cfieldkey.']', $cfieldvalue);
          echo '</label>';
        }
      }
    }
    echo '<input type="submit" value="Wyślij formularz" />';
    echo '</form>';
  }
}
else
  echo 'Oferta niedostępna!';
 
?>
  </body>
</html>

auxiliary Library

The auxiliary library used in both the above examples.

mdswsBooking.class.php
<?php
/**
 * helper class
 */
class mdswsBookings extends XMLWriter
{
  public function genXMLRQ($auth, $type='check', $params, $formdata=array())
  {
    $this->openMemory();
    $this->setIndent(true);
    $this->setIndentString('    ');
    $this->startDocument('1.0', 'UTF-8');
    $this->startElement('mds');
 
    $this->startElement('auth');
    $this->writeElement('login', $auth['login']);
    $this->writeElement('pass', $auth['pass']);
    $this->endElement(); // </auth>
 
    $this->startElement('request');
    $this->writeElement('type', $type);
 
    $this->startElement('conditions');
    if (is_array($params))
      foreach ($params as $pkey=>$pvalue)
        $this->writeElement($pkey, $pvalue);
    $this->endElement(); // </conditions>
 
    if (isset($formdata) && is_array($formdata) && count($formdata))
    {
      $this->write_array_to_xml('forminfo', $formdata);
    }
 
    $this->endElement(); // </request>
 
    $this->endElement(); // </mds>
 
    return $this->outputMemory(true);
  }
  public function sendXML($rqxml)
  {
    $c = curl_init('http://mdsws.merlinx.pl/bookV3/');
    curl_setopt($c, CURLOPT_POST, true);
    curl_setopt($c, CURLOPT_POSTFIELDS, $rqxml);
    curl_setopt($c, CURLOPT_ENCODING, 'gzip');
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    $res = curl_exec($c);
    curl_close($c);
    return $res;
  }
  private function write_array_to_xml($inpname, $inpvalue)
  {
    if (is_numeric($inpname))
      $inpname = 'data';
 
    if (is_array($inpvalue))
    {
      $this->startElement($inpname);
      foreach ($inpvalue as $ikey=>$ivalue)
        $this->write_array_to_xml($ikey,$ivalue);
      $this->endElement();
    }
    else
      $this->writeElement($inpname, $inpvalue);
  }
  /**
   * @param SimpleXML object
   */
  public function processFormData(&$xml)
  {
    $resarr = array();
    if (count($xml->children()))
    {
      foreach ($xml->children() as $nname => $child)
      {
        switch ($nname) {
          case 'data':
            $resarr[] = $this->processFormData($child);
            break;
          default:
            $resarr[$nname] = $this->processFormData($child);
          break;
        }
      }
    }
    else
      $resarr = (string)$xml;
    return $resarr;
  }
 
  /**
   * @param Array
   */
  public function filterFormValues($arr)
  {
    if (is_array($arr) && count($arr))
    {
      if (isset($arr['value']))
        $arr = $arr['value'];
      elseif (isset($arr['selected']))
        $arr = $arr['selected'];
      elseif (isset($arr['values']))
        $arr = $arr['values'][0];
      else
      {
        foreach ($arr as $key=>$value)
          $arr[$key] = $this->filterFormValues($value);
      }
    }
    return $arr;
  }
 
}
 
?>
booking/examples.txt · Last modified: 2015/09/18 09:39 by marekj