×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Jing Tai Piao
Added: Aug 5, 2013 2:40 AM
Views: 52
Tags: no tags
  1. <?php
  2.  
  3. /*
  4.  * To change this template, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7.  
  8. class V3Soap extends SoapClient {
  9.     private $timeout;
  10.    
  11.     public function __construct($wsdl, $endpoint) {
  12.  
  13.         try {
  14.             if (!file_get_contents($wsdl)) {
  15.                 throw new Exception('SOAP-ERROR: Parsing WSDL:  Could not load from WSDL');
  16.             }
  17.             parent::__construct($wsdl, array('location' => $endpoint, 'trace' => 1));
  18.         } catch (Exception $e) {
  19.             throw new Exception('SOAP-ERROR: Parsing WSDL:  Could not load from WSDL');
  20.             exit;
  21.         }
  22.     }
  23.  
  24.     public function __setTimeout($timeout) {
  25.         if (!is_int($timeout) && !is_null($timeout)) {
  26.             throw new Exception("Invalid timeout value");
  27.         }
  28.         $this->timeout = $timeout;
  29.     }
  30.  
  31.     public function __doRequest($request, $location, $action, $version, $one_way = FALSE) {
  32.         if (!$this->timeout) {
  33.             // Call via parent because we require no timeout
  34.             $response = parent::__doRequest($request, $location, $action, $version, $one_way);
  35.         } else { // Call via Curl and use the timeout
  36.             $curl = curl_init($location);
  37.             file_put_contents($_SERVER['DOCUMENT_ROOT'].'/logs/booking.log', 'OOOOOOOOO');
  38.             curl_setopt($curl, CURLOPT_VERBOSE, FALSE);
  39.             curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  40.             curl_setopt($curl, CURLOPT_POST, TRUE);
  41.             curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
  42.             curl_setopt($curl, CURLOPT_HEADER, FALSE);
  43.             curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
  44.             curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
  45.             $response = curl_exec($curl);
  46.             if (curl_errno($curl)) {
  47.                 throw new Exception(curl_error($curl));
  48.             }
  49.             curl_close($curl);
  50.         }
  51.         //Return?
  52.         if (!$one_way) {
  53.             return ($response);
  54.         }
  55.     }
  56.  
  57. //   public  function __doRequest($request, $location, $action, $version) {
  58. //      $dom = new DOMDocument('1.0');
  59. //
  60. //      try {
  61. //
  62. //         //loads the SOAP request to the Document
  63. //         $dom->loadXML($request);
  64. //      } catch (DOMException $e) {
  65. //         die('Parse error with code ' . $e->code);
  66. //      }
  67. //
  68. //      //create a XPath object to query the request
  69. //      $path = new DOMXPath($dom);
  70. //
  71. //      //search for a node
  72. //      $nodesToFix = $path->query('//SOAP-ENV:Envelope/SOAP-ENV:Body/path/to/node');
  73. //
  74. //      //check if nodes are ok
  75. //      $this->checkNodes($path, $nodesToFix);
  76. //
  77. //      //save the modified SOAP request
  78. //      $request = $dom->saveXML();
  79. //      //doRequest
  80. //      return parent::__doRequest($request, $location, $action, $version);
  81. //   }
  82.  
  83.     public function checkNodes(DOMXPath $path, DOMNodeList $nodes) {
  84.         //iterate through the node list
  85.         for ($i = 0; $i < $nodes->length; $i++) {
  86.             $aNode = $nodes->item($i);
  87.  
  88.             //just an example
  89.             if ($node->nodeValue == null) {
  90.                 //do something. For instance, let's remove it.
  91.                 $node->parentNode->removeChild($node);
  92.             }
  93.         }
  94.     }
  95.  
  96. }
  97.  
  98. ?>