Logo
XML Schnittstelle 23.0

XML Connector-Class 23.0

Für den Einsatz mit der Schnittstelle 23.0

PHP Source Code
<?
/*
Hinweis: Die Klasse bildet nur einen Rumpf für einen klassischen Webshop ab und bildet nicht alle Möglichkeiten der API ab.
* Die API kann auch direkt via CURL POST / Get angesprochen werden ohne das Klassenkonstrukt
* Die API kann auch auf json umgestellt werden - dieses Beispiel zeigt nur die Verarbeitung via XML
*/
class connector {
    // Connection Variables
    private $id_user         = ""; # Kundennummer des Weinguts - User identifikator (ohne w) int
    private $api_user        = ""; # API_User Credentials
    private $api_code         = ""; # API Code
    private $api_shop_id      = ""; # ID des Shops 
    private $language         = ""; # dev - ignore # not yet implemented
    private $root            = ""; // The API to connect to 
    public  $debug             = false; // Aktiviert Debug Ausgaben für URL-Check
    public  $debug_data        = array(); # Dump Var wird in shop_core.inc.php am Ende ausgegeben wenn debug ture ist
    private $request_url    = ""; // Request URL to Nephele XML 
    private $IS_SHOP_CACHE = true; // Enables Caching to mimize API Calls / Should be false in Development Mode
    
    # Config Data loaded / cached from the XML
    private $shopconfig     = NULL;
    private $shopgutschein     = NULL;
    private $shopweindetail = array(); 
    private $winelist         = NULL;
    private $menue             = NULL;
    private $agb             = NULL;
    private $zimmerbelegung = NULL;
    private $newsprev         = NULL;
    private $news             = array();
    public  $start             = 0.00;
    private $now             = 0.00;
    private $cacheTime         = 120.0;
 


    // Contructor to set up the api #############################################################
    function __construct($id_user,$api_user,$api_code,$api_shop_id,$language="DE") {
         
        # Endpoint for v23.0 
        $this->root = "https://weinstore.net/xml/v23.0/wbo-API.php";

        # Optional For performance Testing
        $this->start = microtime(true);
        $this->now = microtime(true);

        if($id_user!="" && $api_user !="" && $api_code!="" && $api_shop_id!="") {
            $this->id_user         = trim($id_user);
            $this->api_user     = trim($api_user);
            $this->api_code     = trim($api_code);
            $this->api_shop_id     = trim($api_shop_id);        
            $this->language        = trim($language); # not yet implemented
            
            # Basic Request URL
            $this->request_url  = $this->root;
            $this->request_url.= "?UID=".$this->id_user;
            $this->request_url.= "&apiUSER=".$this->api_user;
            $this->request_url.= "&apiCODE=".$this->api_code;
            $this->request_url.= "&apiShopID=".$this->api_shop_id;
            $this->request_url.= "&encoding=";
            $this->request_url.= "&apiACTION=";
            
            return true;
        } else {
            return false;
        }    
    } 
    
    
    
    //Getter####################################################################################
    
    # Allgemeine Shop-Config
    function getShopConfig(){
        $cache = $this->getCachedItem("shopconfig");

        if (!is_null($cache)) {
            return $cache;
        }
        $url = $this->request_url . "getEinstellungen";
        $result = $this->perform_xml_request($url);
        $this->setCachedItem("shopconfig", $result);
        return $result;
    }
    
    # Warengruppen abrufen
    function getWarengruppen(){
        $cache = $this->getCachedItem("shopWarengruppen");

        if (!is_null($cache)) {
            return $cache;
        }
        $url = $this->request_url . "getWarengruppen";
        $result = $this->perform_xml_request($url);
        $this->setCachedItem("shopWarengruppen", $result);
        return $result;
    }
    
    # Warengruppen-Kategorien abrufen
    function getWarengruppenKategorien(){
        $cache = $this->getCachedItem("shopWarengruppenKategorien");

        if (!is_null($cache)) {
            return $cache;
        }
        $url = $this->request_url . "getWarengruppenKategorien";
        $result = $this->perform_xml_request($url);
        $this->setCachedItem("shopWarengruppenKategorien", $result);
        return $result;
    }

    # Vorschau für News abrufen - Count ist die Maximale Anzahl
    function getNewsPrev($count=200){
        $cache = $this->getCachedItem("newsprev");
        if (!is_null($cache)) {
            return $cache;
        }
        $url = $this->request_url . "getNewsPrev&news_count=" . $count;
        $result = $this->perform_xml_request($url);
        $this->setCachedItem("newsprev", $result);
        return $result;
    }

    # Details zu einem Newseintrag abrufen
    function getNews($news_id){
        $cache = $this->getCachedItem("news", $news_id);
        if (!is_null($cache)) {
            return $cache;
        }
        $url = $this->request_url . "getNews&news_id=" . $news_id;
        $result = $this->perform_xml_request($url);
        $this->setCachedItem("news", $result, $news_id);
        return $result;
    }

    # Gutscheine Abrufen
    function getGutscheine(){
        $cache = $this->getCachedItem("shopgutschein");
        if (!is_null($cache)) {
            return $cache;
        }
        $url = $this->request_url . "getGutscheine";
        $result = $this->perform_xml_request($url);
        $this->setCachedItem("shopgutschein", $result);
        return $result;
    }

    # Expertisendaten abrufen
    function getExpertise($id_expertise){
        $url     = $this->request_url."getExpertise&id_expertise=".$id_expertise;
        $result = $this->perform_xml_request($url,true);
        $this->shopgutschein = $result;
        return $result;    
    }
    
    
    // get wine information data, immer mit bestand und übermandanten, beides wird in function über config gesteuert
    function getWeinDetail($weinnr, $bestand="true", $bestand_mandanten = "false") {
        $params = $weinnr."&bestand=".$bestand."&firmenverbund=".$bestand_mandanten;

        $cache = $this->getCachedItem("shopweindetail","", $params );
        if (!is_null($cache)) {
            return $cache;
        }

        $url = $this->request_url . "getWineDetail&weinnr=" . trim($weinnr) . "&bestand=" . $bestand."&firmenverbund=".$bestand_mandanten;
        $result = $this->perform_xml_request($url);
        $this->setCachedItem("shopweindetail", $result->item,"",  $params);
        return $result->item;
        
    }
    // Get categories for the shop with displayable content
    function getShopMenue() {
        $cache = $this->getCachedItem("menue");
        if (!is_null($cache)) {
            return $cache;
        }
        $url = $this->request_url . "getWineGroups";
        $result = $this->perform_xml_request($url);
        $this->setCachedItem("menue", $result);
        return $result;
    }
    
    // Get AGB from WBO
    function getAGB() {
        $cache = $this->getCachedItem("agb");
        if (!is_null($cache)) {
            return $cache;
        }
        $url = $this->request_url . "getAGB";

        $result = $this->perform_xml_request($url);
        $this->setCachedItem("agb", $result);
        return $result;
    }
    
    // Get List of Wines -> group_id=0 gets all wines // Suchstring für Einschränkungen
    function getWineList($group_id="",$suchstring=false, $bestand = "true", $bestand_mandanten  = "false") {
        //suchstring hat default false, wenn dann ist suchstring leer für die api, ansosnten wird ein string übergeben
        if (!$suchstring){
            $suchstring = "";
        }
        $cacheParams  = "&grp_id=".$group_id."&bestand=".$bestand."&bestand_mandanten=".$bestand_mandanten."&suchstring=".$suchstring ;
        $cache = $this->getCachedItem("winelist","", $cacheParams);

        if (!is_null($cache)) {
            return $cache;
        }
        $url = $this->request_url . "getWines&id_grp=" . $group_id . "&suchstring=" . $suchstring . "&bestand=" . $bestand."&firmenverbund=".$bestand_mandanten;
        $url = $this->decode_url($url);
        $result = $this->perform_xml_request($url);
        $this->setCachedItem("winelist", $result, "",$cacheParams );

        return $result;
    }
    
    # Belegungsdaten zu einem Zimmer abrufen
    function getZimmerBelegung($artikelnr="") {

        $cache = $this->getCachedItem("zimmerbelegung");
        if (!is_null($cache)) {
            return $cache;
        }
        $url = $this->request_url . "getZimmerStatus&artikelnr=" . $artikelnr;
        $url = $this->decode_url($url);
        $result = $this->perform_xml_request($url);
        $this->setCachedItem("zimmerbelegung", $result);
        return $result;
    }
    
    // Send data ##################################################################################
    
    // Do a new Order
    function sendOrder($firma,$name,$nname,$email,$street,$number,$zip,$city,$country,$phone,$l_city,$l_hnumber,$l_adr,$l_anr,$l_company,$l_vorname,$l_name,$l_street,$l_zip,$l_country,$payment,$note,$positions,$array,$versandkosten,$ktname,$ktblz,$ktnr,$gebuehr,$ktiban,$ktbic,$anrede,$paypal_transaction_id,$gutscheincode,$gutscheinwert,$sendDoi=0,$lieferarten,$geburtstag) {
        // $array needs amount and id informations -> Array([&wein_anzahl1=5&wein_id1=12],[&wein_anzahl2=3...)
        $url = $this->request_url."newOrder";
        
        // General adress information
        $url .= "&anrede=$anrede";
        $url .= "&name=$name";
        $url .= "&nname=$nname";
        $url .= "&email=$email";
        $url .= "&strasse=$street";
        $url .= "&hnr=$number";
        $url .= "&plz=$zip";
        $url .= "&ort=$city";
        $url .= "&land=$country";
        $url .= "&telefon=$phone";
        $url .= "&firma=$firma";
        $url .= "&sendDoi=".$sendDoi;
        $url .= "&zahlung=$payment";
        
        // alternate adress information for seperate shipping
        $url .= "&l_adr =$l_adr";
        $url .= "&l_anrede=$l_anr";
        $url .= "&l_firma=$l_company";
        $url .= "&l_vorname=$l_vorname";
        $url .= "&l_name=$l_name";
        $url .= "&l_strasse=$l_street";
        $url .= "&l_plz=$l_zip";
        $url .= "&l_ort=$l_city";
        $url .= "&l_land=$l_country";
        $url .= "&l_hnummer=$l_hnumber";
        $url .= "&id_lieferart=$lieferarten";
        
        $url .= "&kto=".$ktnr;
        $url .= "&ktoInh=".$ktname;
        $url .= "&blz=".$ktblz;
        $url .= "&iban=".$ktiban;
        $url .= "&bic=".$ktbic;
        $url .= "&gutscheincode=".$gutscheincode;
        $url .= "&gutscheinwert=".$gutscheinwert;
        $url .= "&geburtstag=".$geburtstag;
        $url .= "&quelle=".$_SESSION["quelle"];
        $url .= "&device=".$$_SESSION["device"];
        
        // Payment
            if($payment == "Vorkasse") $url .= "&zahlung=6";
            else if($payment == "Rechnung")$url .= "&zahlung=1";
            else if($payment== "Bar")$url .= "&zahlung=5";
            else if($payment == "Nachnahme")$url .= "&zahlung=21";
            else if($payment == "Bankeinzug")$url .= "&zahlung=3";
            else if($payment == "PayPal")$url .= "&zahlung=4";
            else if($payment == "Kreditkarte")$url .= "&zahlung=9";
            else if($payment == "Stripe")$url .= "&zahlung=31";
        else if($payment == "Sofortueberweisung")$url .= "&zahlung=8";
        
        $url .= "&gebuehr=$gebuehr";// Nachnahmegebühr ggf.

        // Note
        $note = preg_replace('/\\r/', ' ', $note); // Umbrüche entfernen
        $note = preg_replace('/\\n/', ' ', $note);

        if($paypal_transaction_id!="") {
            $note.="  Transaktions-ID: ".$paypal_transaction_id;
            $url.= "&woo_transaktions_code=".$paypal_transaction_id;
        }

        $url .= "&referenz=$note";
        
        // versandkosten
        $url.="&versandkosten=$versandkosten";
        
        // Number of positions
        $url .= "&positionen=$positions";
        $url .=implode("",$array);
                
        // perfom request
        $url     =  $this->decode_url($url);
        //echo $url;
        $xml = $this->perform_xml_request($url);

        // Analyze Result
        if($xml->status == "ok") {
            return true;
        } else {
            return false; 
        }
    }
    
    # Getter für den Status des Auftrags
    function getAuftragStatus($auftragnummer, $email){
        $url = $this->request_url . "getAuftragStatus";
        $url.="&auftragnummer=".$auftragnummer;
        $url.="&email=".$email;

        $result = $this->perform_xml_request($url);
        
        return $result;
    }

    # Setzen eines Auftrags / Status
    function setAuftragStatus($auftragnummer, $status, $art, $widerruf_info="",  $send_widerruf_mail=0){
        
        $ip = "";    
        if($_SERVER["REMOTE_ADDR"]!="")              $ip = ($_SERVER["REMOTE_ADDR"]);
        if($_SERVER["HTTP_X_REAL_IP"]!="")              $ip = ($_SERVER["HTTP_X_REAL_IP"]);
        if($_SERVER["HTTP_X_FORWARDED_FOR"]!="")      $ip = ($_SERVER["HTTP_X_FORWARDED_FOR"]);
        if($_SERVER["HTTP_CLIENT_IP"]!="")              $ip = ($_SERVER["HTTP_CLIENT_IP"]);
        
        $url = $this->request_url . "setAuftragStatus";
        $url.="&auftragnummer=".$auftragnummer;
        $url.="&auftrag_status=".$status;
        $url.="&auftrag_status_art=".$art;
        $url.="&send_widerruf_mail=".$send_widerruf_mail;
        $url.="&widerruf_ip=".$ip;
        $url.="&widerruf_info=".$widerruf_info;
        $result = $this->perform_xml_request($url);
        return $result;

    }
    
    
    //Caching Funktonen #######################################################################################################################
    
    # Caching Funktion um Abfragen zu minimieren # ggf. deaktivieren über globale Einstellung gerade beim Development
    function getCachedItem($name, $arr = "", $params = "") {
        if (!$this->IS_SHOP_CACHE)  return null;
        session_start();
        $cachstring = "shopcache_".$this->id_user."_".$this->api_shop_id ."_". $name . $arr . $params;
        if (!is_null($GLOBALS[$cachstring])) {
            if ((microtime(true) - (float)$GLOBALS[$cachstring]["zeit"]) < $this->cacheTime) {
                if (!is_null($GLOBALS[$cachstring]["value"]) ) {
                    $value = null;
                    try {
                        $value = new SimpleXMLElement($GLOBALS[$cachstring]["value"]);
                    }catch (Exception $exception){
                        $value = null;
                    }
                    return $value;

                } else {
                    return null;
                }

            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    # Caching-Funktion setter
    function setCachedItem($name, $value, $arr = "", $params = "") {
        if ($this->IS_SHOP_CACHE) {
            session_start();
            $cachstring = "shopcache_".$this->id_user."_".$this->api_shop_id ."_". $name . $arr . $params;
            if ($value instanceof SimpleXMLElement  ) {
                $value = $value->asXML();
            }
            if (trim((string)$value) != ''  ) {

                $GLOBALS[$cachstring] = [
                    "zeit" => microtime(true),
                    "value" => $value,
                    "name" => $name,
                    "arr" => $arr
                ];
            }
        }
    }
    
    // General Function ########################################################################
    // Prepare a URL for a XML request 
    function decode_url ($url) {
              $lcSearch = Array(
              "#",
              "+",
              "ä",
              "Ä",
              "ö",
              "Ö",
              "ü",
              "Ü",
              "ß",
              " "
              );
              $lcReplace = Array(
              "",
              "%2b",
              "%E4",
              "%C4",
              "%F6",
              "%D6",
              "%FC",
              "%DC",
              "%DF",
              "%20"
              );
              return str_replace($lcSearch,$lcReplace,$url);        
    }
    
    
    // Perform a XML Request
    function perform_xml_request($url,$expertise=false){

        $time = microtime(true) - $this->now;
        $this->now = microtime(true);
        $url = $this->decode_url($url);

        ########################
        if ($this->debug) {
            $dump = array();
            $dump ["Zeit"] = $time;
            $dump ["Art"] = "Anfrage";
            $dump ["URL"] = $url;
            $dump ["trace"] = debug_backtrace();
            $this->debug_data[] = $dump;
        }
        #######################

        $curl = curl_init($url);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Get the response as a string
        curl_setopt($curl, CURLOPT_HEADER, true);         // Include headers in the output

        $response = curl_exec($curl);

        if ($response === false) die("Fehler 194: " );
       
        $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
        $body = substr($response, $headerSize);

        $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);  // Get the HTTP status code

        curl_close($curl);

        $antwort = "";
        if ($httpStatus !== 204) {
            $antwort = $body;
        }

        // Convert Information Data
        if ($expertise) {
            $xml = $antwort;
        } else {
            if (empty($antwort)) {
                $xml = new SimpleXMLElement('<root></root>');
            } else {
                $xml = simplexml_load_string($antwort);
                if ($xml === false) {
                    die("Fehler #01: Kein wohlgeformtes XML gefunden. ");
                }
            }
        }
        ########################
        if ($this->debug) {
            $time = microtime(true) - $this->now;
            $this->now = microtime(true);
            $dump = array();
            $dump ["Zeit"] = $time;
            $dump ["Art"] = "Antwort";
            $dump ["URL"] = $antwort;
            $dump ["trace"] = debug_backtrace();
            $this->debug_data[] = $dump;
        }
        #######################

        // Return  Element
        return $xml;
    }
}
?>