Hoa central
Client.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\XmlRpc;
38 
39 use Hoa\Socket;
40 
49 class Client
50 {
56  protected $_client = null;
57 
63  protected $_script = null;
64 
65 
66 
75  public function __construct(Socket\Client $client, $script)
76  {
77  $this->_client = $client;
78  $this->_script = $script;
79  $client->connect();
80 
81  return;
82  }
83 
90  public function getHeader($message)
91  {
92  return
93  'POST /' . $this->getScript() . ' HTTP/1.1' . "\r\n" .
94  'User-Agent: Hoa' . "\r\n" .
95  'Host: ' . $this->_client->getSocket()->getAddress() . "\r\n" .
96  'Content-Type: text/xml' . "\r\n" .
97  'Content-Length: ' . strlen($message) . "\r\n" .
98  "\r\n" .
99  $message;
100  }
101 
109  public function send(Message\Request $message)
110  {
111  $request = $message->__toString();
112  $this->_client->writeAll($this->getHeader($request));
113  $response = $this->_client->readAll();
114 
115  if (false === $pos = strpos($response, "\r\n\r\n")) {
116  throw new Exception(
117  'Oops, an unknown error occured. Headers seem to be corrupted.',
118  0
119  );
120  }
121 
122  $response = substr($response, $pos + 4);
123 
124  if (0 !== preg_match('#<methodResponse>(\s|\n)*<fault>#i', $response)) {
125  preg_match(
126  '#<(i4|int)>(?:\s|\n)*(\d+)(?:\s|\n)*</\1>#i',
127  $response,
128  $faultCodeMatches
129  );
130  preg_match(
131  '#<string>(?:\s|\n)*(.*)(?:\s|\n)*</string>#i',
132  $response,
133  $faultStringMatches
134  );
135 
136  $faultCode = -1;
137  $faultString = 'An ununderstable fault from the server occured.';
138 
139  if (isset($faultCodeMatches[2])) {
140  $faultCode = $faultCodeMatches[2];
141  }
142 
143  if (isset($faultStringMatches[1])) {
144  $faultString = $faultStringMatches[1];
145  }
146 
147  throw new Exception\Fault($faultString, $faultCode, $request);
148  }
149 
150  return new Message\Response($response);
151  }
152 
158  public function getScript()
159  {
160  return $this->_script;
161  }
162 }
getHeader($message)
Definition: Client.php:90
__construct(Socket\Client $client, $script)
Definition: Client.php:75
send(Message\Request $message)
Definition: Client.php:109