TCP.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Shane Hanna <iordy_at_iordy_dot_com> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: TCP.php,v 1.3 2003/04/13 21:38:58 shane Exp $
  20. //
  21. require_once 'SOAP/Base.php';
  22. /**
  23. * TCP Transport for SOAP
  24. *
  25. * TODO:
  26. * use Net_Socket
  27. * implement some security scheme
  28. * implement support for attachments
  29. *
  30. *
  31. * @access public
  32. * @package SOAP::Transport::TCP
  33. * @author Shane Hanna <iordy_at_iordy_dot_com>
  34. */
  35. class SOAP_Transport_TCP extends SOAP_Base_Object
  36. {
  37. var $headers = array();
  38. var $urlparts = NULL;
  39. var $url = '';
  40. var $incoming_payload = '';
  41. var $_userAgent = SOAP_LIBRARY_NAME;
  42. var $encoding = SOAP_DEFAULT_ENCODING;
  43. var $result_encoding = 'UTF-8';
  44. var $result_content_type;
  45. # socket
  46. var $socket = '';
  47. /**
  48. * SOAP_Transport_TCP Constructor
  49. *
  50. * @param string $URL http url to soap endpoint
  51. *
  52. * @access public
  53. */
  54. function SOAP_Transport_TCP($URL, $encoding = SOAP_DEFAULT_ENCODING)
  55. {
  56. parent::SOAP_Base_Object('TCP');
  57. $this->urlparts = @parse_url($URL);
  58. $this->url = $URL;
  59. $this->encoding = $encoding;
  60. }
  61. function _socket_ping () {
  62. // XXX how do we restart after socket_shutdown?
  63. //if (!$this->socket) {
  64. # create socket resource
  65. $this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  66. if ($this->socket < 0) return 0;
  67. # connect
  68. $result = socket_connect($this->socket, $this->urlparts['host'], $this->urlparts['port']);
  69. if ($result < 0) return 0;
  70. //}
  71. return 1;
  72. }
  73. /**
  74. * send and receive soap data
  75. *
  76. * @param string &$msg outgoing post data
  77. * @param string $action SOAP Action header data
  78. *
  79. * @return string|fault response
  80. * @access public
  81. */
  82. function &send(&$msg, $options = NULL)
  83. {
  84. $this->incoming_payload = '';
  85. $this->outgoing_payload = &$msg;
  86. if (!$this->_validateUrl()) return $this->fault;
  87. # check for TCP scheme
  88. if (strcasecmp($this->urlparts['scheme'], 'TCP') == 0) {
  89. # check connection
  90. if (!$this->_socket_ping())
  91. return $this->_raiseSoapFault('error on '.$this->url.' reason '.socket_strerror(socket_last_error($this->socket)));
  92. # write to the socket
  93. if (!@socket_write($this->socket, $this->outgoing_payload, strlen($this->outgoing_payload))) {
  94. return $this->_raiseSoapFault('Error sending data to '.$this->url.' reason '.socket_strerror(socket_last_error($this->socket)));
  95. }
  96. # shutdown writing
  97. if(!socket_shutdown($this->socket, 1))
  98. return $this->_raiseSoapFault("can't change socket mode to read.");
  99. # read everything we can.
  100. while($buf = @socket_read($this->socket, 1024, PHP_BINARY_READ)) {
  101. $this->incoming_payload .= $buf;
  102. }
  103. # return payload or die
  104. if ($this->incoming_payload)
  105. return $this->incoming_payload;
  106. return $this->_raiseSoapFault("Error reveiving data from ".$this->url);
  107. }
  108. return $this->_raiseSoapFault('Invalid url scheme '.$this->url);
  109. }
  110. // private members
  111. /**
  112. * validate url data passed to constructor
  113. *
  114. * @return boolean
  115. * @access private
  116. */
  117. function _validateUrl()
  118. {
  119. if ( ! is_array($this->urlparts) ) {
  120. $this->_raiseSoapFault("Unable to parse URL $url");
  121. return FALSE;
  122. }
  123. if (!isset($this->urlparts['host'])) {
  124. $this->_raiseSoapFault("No host in URL $url");
  125. return FALSE;
  126. }
  127. if (!isset($this->urlparts['path']) || !$this->urlparts['path'])
  128. $this->urlparts['path'] = '/';
  129. return TRUE;
  130. }
  131. }
  132. ?>