SOAP
[ class tree: SOAP ] [ index: SOAP ] [ all elements ]

Source for file TCP.php

Documentation is available at TCP.php

  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. {
  36.     /**
  37.      * Socket.
  38.      */
  39.     var $socket = null;
  40.  
  41.     /**
  42.      * Constructor.
  43.      *
  44.      * @param string $url  HTTP url to SOAP endpoint.
  45.      *
  46.      * @access public
  47.      */
  48.     function SOAP_Transport_TCP($url$encoding = SOAP_DEFAULT_ENCODING)
  49.     {
  50.         parent::SOAP_Base_Object('TCP');
  51.         $this->urlparts = @parse_url($url);
  52.         $this->url = $url;
  53.         $this->encoding = $encoding;
  54.     }
  55.  
  56.     function _socket_ping()
  57.     {
  58.         // XXX how do we restart after socket_shutdown?
  59.         //if (!$this->socket) {
  60.             // Create socket resource.
  61.             $this->socket = @socket_create(AF_INETSOCK_STREAMSOL_TCP);
  62.             if ($this->socket < 0{
  63.                 return 0;
  64.             }
  65.  
  66.             // Connect.
  67.             $result socket_connect($this->socket$this->urlparts['host'],
  68.                                      $this->urlparts['port']);
  69.             if ($result < 0{
  70.                 return 0;
  71.             }
  72.         //}
  73.         return 1;
  74.     }
  75.  
  76.     /**
  77.      * Sends and receives SOAP data.
  78.      *
  79.      * @access public
  80.      *
  81.      * @param string  Outgoing SOAP data.
  82.      * @param array   Options.
  83.      *
  84.      * @return string|SOAP_Fault
  85.      */
  86.     function send($msg$options = array())
  87.     {
  88.         $this->fault = null;
  89.         $this->incoming_payload = '';
  90.         $this->outgoing_payload = $msg;
  91.         if (!$this->_validateUrl()) {
  92.             return $this->fault;
  93.         }
  94.  
  95.         // Check for TCP scheme.
  96.         if (strcasecmp($this->urlparts['scheme']'TCP'== 0{
  97.             // Check connection.
  98.             if (!$this->_socket_ping()) {
  99.                 return $this->_raiseSoapFault('Error connecting to ' $this->url . '; reason: ' socket_strerror(socket_last_error($this->socket)));
  100.             }
  101.  
  102.             // Write to the socket.
  103.             if (!@socket_write($this->socket$this->outgoing_payload,
  104.                                strlen($this->outgoing_payload))) {
  105.                 return $this->_raiseSoapFault('Error sending data to ' $this->url . '; reason: ' socket_strerror(socket_last_error($this->socket)));
  106.             }
  107.  
  108.             // Shutdown writing.
  109.             if(!socket_shutdown($this->socket1)) {
  110.                 return $this->_raiseSoapFault('Cannot change socket mode to read.');
  111.             }
  112.  
  113.             // Read everything we can.
  114.             while ($buf @socket_read($this->socket1024PHP_BINARY_READ)) {
  115.                 $this->incoming_payload .= $buf;
  116.             }
  117.  
  118.             // Return payload or die.
  119.             if ($this->incoming_payload{
  120.                 return $this->incoming_payload;
  121.             }
  122.  
  123.             return $this->_raiseSoapFault('Error reveiving data from ' $this->url);
  124.         }
  125.  
  126.         return $this->_raiseSoapFault('Invalid url scheme ' $this->url);
  127.     }
  128.  
  129.     /**
  130.      * Validates the url data passed to the constructor.
  131.      *
  132.      * @return boolean 
  133.      * @access private
  134.      */
  135.     function _validateUrl()
  136.     {
  137.         if (!is_array($this->urlparts) ) {
  138.             $this->_raiseSoapFault("Unable to parse URL $this->url");
  139.             return false;
  140.         }
  141.         if (!isset($this->urlparts['host'])) {
  142.             $this->_raiseSoapFault("No host in URL $this->url");
  143.             return false;
  144.         }
  145.         if (!isset($this->urlparts['path']|| !$this->urlparts['path']{
  146.             $this->urlparts['path''/';
  147.         }
  148.  
  149.         return true;
  150.     }
  151.  
  152. }

Documentation generated on Mon, 04 Aug 2008 20:00:32 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.