1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: namespace Jyxo\Rpc\Xml;
15:
16: /**
17: * Class for sending requests using XML-RPC.
18: * Requires xmlrpc and curl PHP extensions.
19: *
20: * @category Jyxo
21: * @package Jyxo\Rpc
22: * @subpackage Xml
23: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
24: * @license https://github.com/jyxo/php/blob/master/license.txt
25: * @author Jaroslav HanslĂk
26: */
27: class Client extends \Jyxo\Rpc\Client
28: {
29: /**
30: * Creates a client instance and eventually sets server address.
31: * Also defines default client settings.
32: *
33: * @param string $url Server address
34: */
35: public function __construct($url = '')
36: {
37: parent::__construct($url);
38:
39: $this->options = array(
40: 'output_type' => 'xml',
41: 'verbosity' => 'pretty',
42: 'escaping' => array('markup'),
43: 'version' => 'xmlrpc',
44: 'encoding' => 'utf-8'
45: );
46: }
47:
48: /**
49: * Sends request and fetches server's response.
50: *
51: * @param string $method Method name
52: * @param array $params Method parameters
53: * @return mixed
54: * @throws \BadMethodCallException If no server address was provided
55: * @throws \Jyxo\Rpc\Xml\Exception On error
56: */
57: public function send($method, array $params)
58: {
59: // Start profiling
60: $this->profileStart();
61:
62: try {
63: // Fetch response
64: $response = $this->process('text/xml', xmlrpc_encode_request($method, $params, $this->options));
65:
66: // Process response
67: $response = xmlrpc_decode($response, 'utf-8');
68:
69: } catch (\Jyxo\Rpc\Exception $e) {
70: // Finish profiling
71: $this->profileEnd('XML', $method, $params, $e->getMessage());
72:
73: throw new Exception($e->getMessage(), 0, $e);
74: }
75:
76: // Finish profiling
77: $this->profileEnd('XML', $method, $params, $response);
78:
79: // Error in response
80: if ((is_array($response)) && (isset($response['faultString']))) {
81: throw new Exception(preg_replace('~\s+~', ' ', $response['faultString']));
82: }
83:
84: return $response;
85: }
86: }
87: