Client.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /* EPP Client class for PHP, Copyright 2005 CentralNic Ltd
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. /**
  16. * A simple client class for the Extensible Provisioning Protocol (EPP)
  17. * @package Net_EPP
  18. * @version 0.0.4
  19. * @author Gavin Brown <gavin.brown@nospam.centralnic.com>
  20. * @revision $Id: Client.php,v 1.13 2010/10/21 11:55:07 gavin Exp $
  21. */
  22. require_once('Protocol.php');
  23. $GLOBALS['Net_EPP_Client_Version'] = '0.0.4';
  24. /**
  25. * A simple client class for the Extensible Provisioning Protocol (EPP)
  26. * @package Net_EPP
  27. */
  28. class Net_EPP_Client {
  29. /**
  30. * @var resource the socket resource, once connected
  31. */
  32. var $socket;
  33. /**
  34. * Establishes a connect to the server
  35. * This method establishes the connection to the server. If the connection was
  36. * established, then this method will call getFrame() and return the EPP <greeting>
  37. * frame which is sent by the server upon connection. If connection fails, then
  38. * an exception with a message explaining the error will be thrown and handled
  39. * in the calling code.
  40. * @param string $host the hostname
  41. * @param integer $port the TCP port
  42. * @param integer $timeout the timeout in seconds
  43. * @param boolean $ssl whether to connect using SSL
  44. * @param resource $context a stream resource to use when setting up the socket connection
  45. * @throws Exception on connection errors
  46. * @return a string containing the server <greeting>
  47. */
  48. function connect($host, $port = 700, $timeout = 1, $ssl = true, $context = NULL) {
  49. $target = sprintf('%s://%s:%d', ($ssl === true ? 'ssl' : 'tcp'), $host, $port);
  50. if (is_resource($context)) {
  51. $result = stream_socket_client($target, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context);
  52. } else {
  53. $result = stream_socket_client($target, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
  54. }
  55. if ($result === False) {
  56. throw new Exception("Error connecting to $target: $errstr (code $errno)");
  57. }
  58. // Set our socket
  59. $this->socket = $result;
  60. // Set stream timeout
  61. if (!stream_set_timeout($this->socket, $timeout)) {
  62. throw new Exception("Failed to set timeout on socket: $errstr (code $errno)");
  63. }
  64. // Set blocking
  65. if (!stream_set_blocking($this->socket, 0)) {
  66. throw new Exception("Failed to set blocking on socket: $errstr (code $errno)");
  67. }
  68. return $this->getFrame();
  69. }
  70. /**
  71. * Get an EPP frame from the server.
  72. * This retrieves a frame from the server. Since the connection is blocking, this
  73. * method will wait until one becomes available. If the connection has been broken,
  74. * this method will return a string containing the XML from the server
  75. * @throws Exception on frame errors
  76. * @return a string containing the frame
  77. */
  78. function getFrame() {
  79. return Net_EPP_Protocol::getFrame($this->socket);
  80. }
  81. /**
  82. * Send an XML frame to the server.
  83. * This method sends an EPP frame to the server.
  84. * @param string the XML data to send
  85. * @throws Exception when it doesn't complete the write to the socket
  86. * @return boolean the result of the fwrite() operation
  87. */
  88. function sendFrame($xml) {
  89. return Net_EPP_Protocol::sendFrame($this->socket, $xml);
  90. }
  91. /**
  92. * a wrapper around sendFrame() and getFrame()
  93. * @param string $xml the frame to send to the server
  94. * @throws Exception when it doesn't complete the write to the socket
  95. * @return string the frame returned by the server, or an error object
  96. */
  97. function request($xml) {
  98. $res = $this->sendFrame($xml);
  99. return $this->getFrame();
  100. }
  101. /**
  102. * Close the connection.
  103. * This method closes the connection to the server. Note that the
  104. * EPP specification indicates that clients should send a <logout>
  105. * command before ending the session.
  106. * @return boolean the result of the fclose() operation
  107. */
  108. function disconnect() {
  109. return @fclose($this->socket);
  110. }
  111. /**
  112. * ping the connection to check that it's up
  113. * @return boolean
  114. */
  115. function ping() {
  116. return (!is_resource($this->socket) || feof($this->socket) ? false : true);
  117. }
  118. }
  119. ?>