Advertisement
Guest User

SOAP Class

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