1
|
|
|
<?php |
2
|
|
|
require_once __DIR__ . "/_prepend.php"; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Demoing how to inject a custom logger for use by the library |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use PhpXmlRpc\Client; |
9
|
|
|
use PhpXmlRpc\Encoder; |
10
|
|
|
use PhpXmlRpc\Helper\Charset; |
11
|
|
|
use PhpXmlRpc\Helper\Http; |
12
|
|
|
use PhpXmlRpc\Helper\XMLParser; |
|
|
|
|
13
|
|
|
use PhpXmlRpc\Request; |
14
|
|
|
|
15
|
|
|
// Definition of a custom logger implementing the same API as the default one |
16
|
|
|
|
17
|
|
|
class MyLogger |
18
|
|
|
{ |
19
|
|
|
protected $debugBuffer = ''; |
20
|
|
|
protected $errorBuffer = ''; |
21
|
|
|
|
22
|
|
|
// logger API |
23
|
|
|
public function debug($message, $context = array()) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$this->debugBuffer .= $message . "\n"; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// logger API |
29
|
|
|
public function error($message, $context = array()) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$this->errorBuffer .= $message . "\n"; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getDebug() |
35
|
|
|
{ |
36
|
|
|
return $this->debugBuffer; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getError() |
40
|
|
|
{ |
41
|
|
|
return $this->errorBuffer; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// create the custom logger instance |
46
|
|
|
|
47
|
|
|
$logger = new MyLogger(); |
48
|
|
|
|
49
|
|
|
// inject it into all the classes (possibly) involved |
50
|
|
|
|
51
|
|
|
Charset::setLogger($logger); |
52
|
|
|
Client::setLogger($logger); |
53
|
|
|
Encoder::setLogger($logger); |
54
|
|
|
Http::setLogger($logger); |
55
|
|
|
Request::setLogger($logger); |
56
|
|
|
XMLParser::setLogger($logger); |
57
|
|
|
|
58
|
|
|
// then send a request |
59
|
|
|
|
60
|
|
|
$input = array( |
61
|
|
|
array('name' => 'Dave', 'age' => 24), |
62
|
|
|
array('name' => 'Edd', 'age' => 45), |
63
|
|
|
array('name' => 'Joe', 'age' => 37), |
64
|
|
|
array('name' => 'Fred', 'age' => 27), |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$encoder = new Encoder(); |
68
|
|
|
$client = new Client(XMLRPCSERVER); |
69
|
|
|
|
70
|
|
|
// set maximum debug level, to have all the communication details logged |
71
|
|
|
$client->setDebug(2); |
72
|
|
|
|
73
|
|
|
// avid compressed responses, as they mess up the output if echoed on the command-line |
74
|
|
|
$client->setAcceptedCompression(''); |
75
|
|
|
|
76
|
|
|
// send request |
77
|
|
|
output("Sending the request. No output debug should appear below...<br>"); |
78
|
|
|
$request = new Request('examples.sortByAge', array($encoder->encode($input))); |
79
|
|
|
$response = $client->send($request); |
80
|
|
|
output("Response received.<br>"); |
81
|
|
|
|
82
|
|
|
output("The client error info is:<pre>\n" . $logger->getError() . "\n</pre>"); |
83
|
|
|
output("The client debug info is:<pre>\n" . $logger->getDebug() . "\n</pre>"); |
84
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: