XRL  2.0.0
Simple XML-RPC Library (both client and server)
Client.php
1 <?php
2 /*
3  * This file is part of XRL, a simple XML-RPC Library for PHP.
4  *
5  * Copyright (c) 2012, XRL Team. All rights reserved.
6  * XRL is licensed under the 3-clause BSD License.
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace fpoirotte\XRL;
13 
15 
47 class Client
48 {
50  protected $baseURL;
51 
53  protected $context;
54 
56  protected $encoder;
57 
59  protected $decoder;
60 
95  public function __construct(
96  $baseURL,
97  \fpoirotte\XRL\EncoderInterface $encoder = null,
98  \fpoirotte\XRL\DecoderInterface $decoder = null,
99  $context = null
100  ) {
101  if ($context === null) {
102  $context = stream_context_get_default();
103  }
104 
105  if (!is_resource($context)) {
106  throw new \InvalidArgumentException('Invalid context');
107  }
108 
109  if ($encoder === null) {
110  $encoder = new \fpoirotte\XRL\NativeEncoder(
111  new \fpoirotte\XRL\Encoder(null, false, true)
112  );
113  }
114 
115  if ($decoder === null) {
116  $decoder = new \fpoirotte\XRL\NativeDecoder(
117  new \fpoirotte\XRL\Decoder(null, true)
118  );
119  }
120 
121  $this->baseURL = $baseURL;
122  $this->context = $context;
123  $this->encoder = $encoder;
124  $this->decoder = $decoder;
125  }
126 
156  public function __call($method, array $args)
157  {
158  $newArgs = array_map('\\fpoirotte\\XRL\\NativeEncoder::convert', $args);
159  $request = new \fpoirotte\XRL\Request($method, $newArgs);
160  $xml = $this->encoder->encodeRequest($request);
161  $options = array(
162  'http' => array(
163  'method' => 'POST',
164  'content' => $xml,
165  'header' => 'Content-Type: text/xml',
166  ),
167  );
168 
169  stream_context_set_option($this->context, $options);
170  libxml_set_streams_context($this->context);
171  return $this->decoder->decodeResponse($this->baseURL);
172  }
173 }
Interface for an XML-RPC decoder.
Interface for an XML-RPC encoder.
A simple XML-RPC client.
Definition: Client.php:47
An XML-RPC encoder that can produce either compact documents or pretty documents. ...
Definition: Encoder.php:21
$encoder
Encoder for the request.
Definition: Client.php:56
$baseURL
The remote XML-RPC server's base URL.
Definition: Client.php:50
$decoder
Decoder for the response.
Definition: Client.php:59
__call($method, array $args)
Definition: Client.php:156
A decoder that can process XML-RPC requests and responses, with optional XML validation.
Definition: Decoder.php:21
$context
A stream context to use when querying the server.
Definition: Client.php:53
__construct($baseURL,\fpoirotte\XRL\EncoderInterface $encoder=null,\fpoirotte\XRL\DecoderInterface $decoder=null, $context=null)
Definition: Client.php:95