<?php

/**
 * The test soap Server is Located @ http://meabed.net/soap/test.php
 */
/** the main soap class */
require_once '../src/SoapClient.php';
/** @var string $wsdl, This is the test server i have generated to test the class */
$wsdl = "http://meabed.net/soap/test.php?WSDL";
/** @var array $options , array of options for the soap client */
$options = array('connection_timeout' => 40, 'trace' => true);
/** @var SoapClientAsync $client New Soap client instance */
$client = new SoapClientAsync($wsdl, $options);
/**
 * You can set debug mode to true to see curl verbose response if you run the script from command line
 * @default false
 */
$client::$debug = true;
/** @var string $session , SessionId required in SoapOperations */
$session = null;
/** Normal ONE SOAP CALL Using CURL same exact as soap synchronous api calls of any web service */
try {
    $loginSoapCall = $client->login(array('demo', '123456'));
    $session = $loginSoapCall->Return;
} catch (SoapFault $ex) {
    print 'SoapFault: ' . $ex->faultcode . ' - ' . $ex->getMessage() . "\n";
} catch (Exception $e) {
    print 'Exception: ' . $ex->faultcode . ' - ' . $ex->getMessage() . "\n";
}
/**
 * set SoapClient Mode to asynchronous mode.
 /**
  * Soap __doRequest() Method with CURL Implementation
  *
  * @param string $request
  * @param string $location
  * @param string $action
  * @param int    $version
  * @param int    $one_way
  *
  * @return string
  * @throws SoapFault
  */
 public function __doRequest($request, $location, $action, $version, $one_way = 0)
 {
     $action = static::$action;
     $actionCommand = static::$action;
     $actionMethod = $action;
     // print xml for debugging testing
     if ($actionCommand != static::GET_RESULT && self::$printSoapRequest) {
         // debug the request here
         echo $this->prettyXml($request);
         if (self::$exitAfterPrint) {
             exit;
         }
         self::$printSoapRequest = false;
         self::$exitAfterPrint = false;
     }
     // some .NET Servers only accept action method with ns url!! uncomment it if you get error wrong command
     // $actionMethod  = str_ireplace(['http://tempuri.org/IFlightAPI/', 'https://tempuri.org/IFlightAPI/'], '', $action);
     /** return the xml response as its coming from normal soap call */
     if ($actionCommand == static::GET_RESULT && static::$xmlResponse) {
         return static::$xmlResponse;
     } else {
     }
     /** return the xml response as its coming from normal soap call */
     if ($action == static::GET_RESULT && static::$xmlResponse) {
         return static::$xmlResponse;
     }
     $soapResponses =& static::$soapResponses;
     $soapRequests =& static::$soapRequests;
     /** @var $id string represent hashId of each request based on the request body to avoid multiple calls for the same request if exists */
     $id = sha1($location . $request);
     /** if curl is not enabled use parent::__doRequest */
     if (!in_array('curl', get_loaded_extensions())) {
         if (isset($soapResponses[$id])) {
             unset($soapResponses[$id]);
             return parent::__doRequest($request, $location, $action, $version, $one_way = 0);
         }
         $soapRequests[$id] = true;
         return "";
     }
     /** return response if soap method called for second time with same parameters */
     if (isset($soapResponses[$id])) {
         $data = $soapResponses[$id];
         unset($soapResponses[$id]);
         if ($data instanceof SoapFault) {
             throw $data;
         }
         return $data;
     }
     /** @var $headers array of headers to be sent with request */
     $headers = ['Content-type: text/xml', 'charset=utf-8', "Accept: text/xml", 'SOAPAction: "' . $action . '"', "Content-length: " . strlen($request)];
     // ssl connection sharing
     if (empty(static::$sharedCurlData[$location])) {
         $shOpt = curl_share_init();
         curl_share_setopt($shOpt, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
         curl_share_setopt($shOpt, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
         curl_share_setopt($shOpt, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
         static::$sharedCurlData[$location] = $shOpt;
     }
     $sh = static::$sharedCurlData[$location];
     $ch = curl_init();
     /** CURL_OPTIONS  */
     curl_setopt($ch, CURLOPT_URL, $location);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_ENCODING, "");
     curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
     curl_setopt($ch, CURLOPT_TIMEOUT, 50);
     curl_setopt($ch, CURLOPT_TIMECONDITION, 50);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_VERBOSE, static::$debug);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLOPT_SHARE, $sh);
     $soapRequests[$id] = $ch;
     static::$requestIds[$id] = $id;
     static::$actions[$id] = $actionMethod;
     static::$requestXml[$id] = $request;
     static::$lastRequestId = $id;
     return "";
 }