home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / SOAP / Transport / TCP.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  4.5 KB  |  154 lines

  1. <?php
  2. /**
  3.  * This file contains the code for a TCP transport layer.
  4.  *
  5.  * PHP versions 4 and 5
  6.  *
  7.  * LICENSE: This source file is subject to version 2.02 of the PHP license,
  8.  * that is bundled with this package in the file LICENSE, and is available at
  9.  * through the world-wide-web at http://www.php.net/license/2_02.txt.  If you
  10.  * did not receive a copy of the PHP license and are unable to obtain it
  11.  * through the world-wide-web, please send a note to license@php.net so we can
  12.  * mail you a copy immediately.
  13.  *
  14.  * @category   Web Services
  15.  * @package    SOAP
  16.  * @author     Shane Hanna <iordy_at_iordy_dot_com>
  17.  * @author     Jan Schneider <jan@horde.org>
  18.  * @copyright  2003-2006 The PHP Group
  19.  * @license    http://www.php.net/license/2_02.txt  PHP License 2.02
  20.  * @link       http://pear.php.net/package/SOAP
  21.  */
  22.  
  23. require_once 'SOAP/Transport.php';
  24.  
  25. /**
  26.  * TCP transport for SOAP.
  27.  *
  28.  * @todo    use Net_Socket; implement some security scheme; implement support
  29.  *          for attachments
  30.  * @access  public
  31.  * @package SOAP
  32.  * @author  Shane Hanna <iordy_at_iordy_dot_com>
  33.  * @author  Jan Schneider <jan@horde.org>
  34.  */
  35. class SOAP_Transport_TCP extends SOAP_Transport
  36. {
  37.     /**
  38.      * Socket.
  39.      */
  40.     var $socket = null;
  41.  
  42.     /**
  43.      * Constructor.
  44.      *
  45.      * @param string $url  HTTP url to SOAP endpoint.
  46.      *
  47.      * @access public
  48.      */
  49.     function SOAP_Transport_TCP($url, $encoding = SOAP_DEFAULT_ENCODING)
  50.     {
  51.         parent::SOAP_Base_Object('TCP');
  52.         $this->urlparts = @parse_url($url);
  53.         $this->url = $url;
  54.         $this->encoding = $encoding;
  55.     }
  56.  
  57.     function _socket_ping()
  58.     {
  59.         // XXX how do we restart after socket_shutdown?
  60.         //if (!$this->socket) {
  61.             // Create socket resource.
  62.             $this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  63.             if ($this->socket < 0) {
  64.                 return 0;
  65.             }
  66.  
  67.             // Connect.
  68.             $result = socket_connect($this->socket, $this->urlparts['host'],
  69.                                      $this->urlparts['port']);
  70.             if ($result < 0) {
  71.                 return 0;
  72.             }
  73.         //}
  74.         return 1;
  75.     }
  76.  
  77.     /**
  78.      * Sends and receives SOAP data.
  79.      *
  80.      * @access public
  81.      *
  82.      * @param string  Outgoing SOAP data.
  83.      * @param array   Options.
  84.      *
  85.      * @return string|SOAP_Fault
  86.      */
  87.     function send($msg, $options = array())
  88.     {
  89.         $this->fault = null;
  90.         $this->incoming_payload = '';
  91.         $this->outgoing_payload = $msg;
  92.         if (!$this->_validateUrl()) {
  93.             return $this->fault;
  94.         }
  95.  
  96.         // Check for TCP scheme.
  97.         if (strcasecmp($this->urlparts['scheme'], 'TCP') == 0) {
  98.             // Check connection.
  99.             if (!$this->_socket_ping()) {
  100.                 return $this->_raiseSoapFault('Error connecting to ' . $this->url . '; reason: ' . socket_strerror(socket_last_error($this->socket)));
  101.             }
  102.  
  103.             // Write to the socket.
  104.             if (!@socket_write($this->socket, $this->outgoing_payload,
  105.                                strlen($this->outgoing_payload))) {
  106.                 return $this->_raiseSoapFault('Error sending data to ' . $this->url . '; reason: ' . socket_strerror(socket_last_error($this->socket)));
  107.             }
  108.  
  109.             // Shutdown writing.
  110.             if(!socket_shutdown($this->socket, 1)) {
  111.                 return $this->_raiseSoapFault('Cannot change socket mode to read.');
  112.             }
  113.  
  114.             // Read everything we can.
  115.             while ($buf = @socket_read($this->socket, 1024, PHP_BINARY_READ)) {
  116.                 $this->incoming_payload .= $buf;
  117.             }
  118.  
  119.             // Return payload or die.
  120.             if ($this->incoming_payload) {
  121.                 return $this->incoming_payload;
  122.             }
  123.  
  124.             return $this->_raiseSoapFault('Error reveiving data from ' . $this->url);
  125.         }
  126.  
  127.         return $this->_raiseSoapFault('Invalid url scheme ' . $this->url);
  128.     }
  129.  
  130.     /**
  131.      * Validates the url data passed to the constructor.
  132.      *
  133.      * @return boolean
  134.      * @access private
  135.      */
  136.     function _validateUrl()
  137.     {
  138.         if (!is_array($this->urlparts) ) {
  139.             $this->_raiseSoapFault("Unable to parse URL $this->url");
  140.             return false;
  141.         }
  142.         if (!isset($this->urlparts['host'])) {
  143.             $this->_raiseSoapFault("No host in URL $this->url");
  144.             return false;
  145.         }
  146.         if (!isset($this->urlparts['path']) || !$this->urlparts['path']) {
  147.             $this->urlparts['path'] = '/';
  148.         }
  149.  
  150.         return true;
  151.     }
  152.  
  153. }
  154.