Advertisement
Guest User

SOAP Class

a guest
Jan 11th, 2016
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.66 KB | None | 0 0
  1. /**
  2.  * Helper class for converting
  3.  */
  4. class XmlElement {
  5.     var $name;
  6.     var $attributes;
  7.     var $content;
  8.     var $children;
  9. };
  10.  
  11. /**
  12.  * General class for web services
  13.  */
  14. class SoapWebServices {
  15.     private $url; //url for web services
  16.     private $client; // Soap global(class) object
  17.  
  18.     function __construct($url) {
  19.         $this->url = $url;
  20.         $this->client = new SoapClient($this->url, array("trace" => 1, "exception" => 0));
  21.     }
  22.     /**
  23.      * Add Soap Header
  24.      * @param stdClass object
  25.      * @param string
  26.      */
  27.     public function add_header($header, $name) {
  28.         $header = new SoapHeader($this->url, $name, $header, false);
  29.         $this->client->__setSoapHeaders(array($header));
  30.     }
  31.  
  32.     /**
  33.      * @param  string   API Method that you want to call
  34.      * @param  array    Arguments to pass into API
  35.      * @param  string   Parent array for arguments
  36.      * @return array    of objects generated from XML
  37.      */
  38.     public function call($func, $args, $parent_arg=NULL) {
  39.         if (!parent_arg) $parent_arg = $func;
  40.         $args = array($parent_arg => $args);
  41.         try {
  42.             $result = $this->client->__soapCall($func, $args, NULL);
  43.         } catch (SoapFault $soap_fault) {
  44.             return $soap_fault;
  45.         }
  46.         $result_func = $func.'Result';
  47.         return $this->xml_to_object($result->$result_func->any);
  48.     }
  49.     /**
  50.      * Get Last Request's XML
  51.      * @return XML
  52.      */
  53.     public function __getLastRequest(){
  54.         if (isset($this->client))
  55.             return $this->client->__getLastRequest();
  56.         else
  57.             return NULL;
  58.     }
  59.  
  60.     /**
  61.      * Convert XML to PHP array of objects
  62.      * @param  XML
  63.      * @return array of objects
  64.      */
  65.     public function xml_to_object($xml) {
  66.         $parser = xml_parser_create();
  67.         xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  68.         xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  69.         xml_parse_into_struct($parser, $xml, $tags);
  70.         xml_parser_free($parser);
  71.  
  72.         $elements = array();  // the currently filling [child] XmlElement array
  73.         $stack = array();
  74.         foreach ($tags as $tag) {
  75.             $index = count($elements);
  76.             if ($tag['type'] == "complete" || $tag['type'] == "open") {
  77.                 $elements[$index] = new XmlElement;
  78.                 $elements[$index]->name = $tag['tag'];
  79.                 $elements[$index]->attributes = isset($tag['attributes'])?$tag['attributes']:NULL;
  80.                 $elements[$index]->content = isset($tag['value'])?$tag['value']:NULL;
  81.                 if ($tag['type'] == "open") {  // push
  82.                     $elements[$index]->children = array();
  83.                     $stack[count($stack)] = &$elements;
  84.                     $elements = &$elements[$index]->children;
  85.                 }
  86.             }
  87.             if ($tag['type'] == "close") {  // pop
  88.                 $elements = &$stack[count($stack) - 1];
  89.                 unset($stack[count($stack) - 1]);
  90.             }
  91.         }
  92.         return $elements[0];  // the single top-level element
  93.     }
  94. }
  95.  
  96. /**
  97.  * High level web services class for "Travel Security Web Services"
  98.  */
  99. class TravelSecurityWebServices extends SoapWebServices {
  100.     private $ts_url = "https://webservices.travelsecurity.com/Default.asmx?WSDL";
  101.     private $username = "asd";
  102.     private $password    = "asd";
  103.     private $membership = "asd";
  104.  
  105.     function __construct(){
  106.         parent::__construct($this->ts_url);
  107.         // TODO:
  108.         // 1. create configuration page in drupal CMS
  109.         // 2. Remove hardcoded credentials and pull them from DB here
  110.     }
  111.  
  112.     public function GetCountryRiskRatings(){
  113.         $args = array(
  114.             "Username" => $this->username,
  115.             "Password" => $this->password,
  116.         );
  117.         return $this->call('GetCountryRiskRatings', $args);
  118.     }
  119.  
  120.     public function GetCountryMedicalRiskRatings($language = 1){
  121.         $args = array(
  122.             "Username" => $this->username,
  123.             "Password" => $this->password,
  124.             "LanguageId" => $language,
  125.         );
  126.         return $this->call('GetCountryMedicalRiskRatings', $args); 
  127.     }
  128.  
  129.     public function GetArticles($language = 1, $country = 30, $tabname = "risk summary"){
  130.         $args = array(
  131.             "Username" => $this->username,
  132.             "Password" => $this->password,
  133.             "languageID" => $language,
  134.             "CountryID" => $country,
  135.             "tabname" => $tabname,
  136.         );
  137.         return $this->call('GetArticles', $args);  
  138.  
  139.     }
  140. }
  141.  
  142. /**
  143.  * High level class for "International SOS Web Services"
  144.  */
  145. class InternationalSOSWebServices extends SoapWebServices {
  146.     private $isos_url   = "https://www.internationalsos.com/ISOS.ServiceCluster.Location.CountryData.WebServices2/CountryGuidesWebService.asmx?WSDL";
  147.     private $username = "asd";
  148.     private $password    = "asd";
  149.     private $membership = "asd";
  150.  
  151.     function __construct(){
  152.         parent::__construct($this->isos_url);
  153.         // TODO:
  154.         // 1. create configuration page in drupal CMS
  155.         // 2. Remove hardcoded credentials and pull them from DB here
  156.     }
  157.  
  158.     public function addHeader(){
  159.         $header = new stdClass();
  160.         $header->MembershipNumber = $this->membership;
  161.         $header->UserName = $this->username;
  162.         $header->Password = $this->password;
  163.         parent::add_header($header, "ISOSAuthenticationHeader");
  164.     }
  165.  
  166.     public function GetCountryGeneralSiteContent($language, $country){
  167.         $this->addHeader();
  168.  
  169.         $args = new stdClass();
  170.         $args->GeneralSiteContentParam = new stdClass();
  171.         $args->GeneralSiteContentParam->LanguageID = $language;
  172.         $args->GeneralSiteContentParam->CountryID = $country;
  173.  
  174.         return $this->call('GetCountryGeneralSiteContent', $args, "GeneralSiteContentParam");  
  175.     }
  176.  
  177.     public function GetCountryMedicalSummary($language, $country){
  178.         $this->addHeader();
  179.  
  180.         $args = new stdClass();
  181.         $args->MedicalSummaryParam = new stdClass();
  182.         $args->MedicalSummaryParam->LanguageID = $language;
  183.         $args->MedicalSummaryParam->CountryID = $country;
  184.  
  185.         return $this->call('GetCountryMedicalSummary', $args, "MedicalSummaryParam");  
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement