Skip to content

Commit 25a6945

Browse files
committed
Updated examples
1 parent 1af7e2b commit 25a6945

File tree

7 files changed

+56
-146
lines changed

7 files changed

+56
-146
lines changed

Diff for: README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Alternatively, you can [download][download] and extract it, or clone this repo.
5050
## Usage
5151
If you downloaded the library through `composer` then everything is ready to run, otherwise you must point a `PSR-0` autoloader to the `src` directory so that the classes are automatically included.
5252

53+
If you just want to have a quick play, point your browser to `example/client.php` and everything will run automatically.
54+
5355
## Client usage
5456
Firstly you need to instantiate a `JsonRpc\Client`. You do this by giving it the url you want to send your requests to:
5557

@@ -64,7 +66,7 @@ Next you send your request by using the `$client->call` function. This takes the
6466
$success = $client->call('method', array($param1, $param2));
6567
```
6668

67-
The function returns true or false. If **true** then the result of the method will be in the `$client->result` property. The type of the result depends on what has been returned by the server, so it could be a scalar, an indexed array, an object `stdClass` or null.
69+
The function returns true or false. If **true** then the result of the method will be in the `$client->result` property. The type of the result depends on what has been returned by the server, so it could be a scalar, an indexed array, an object `stdClass`, or null.
6870

6971
If **false** then an error has occured, either in sending or processing the request. This is reported in the `$client->error` property, which is a string. Putting it all together:
7072

Diff for: example/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
#Example
1+
# Example
22

3-
Run `client.php` in your browser, which communicates with `server.php` in the same directory. Experiment by uncommenting the `/*..*/` blocks to get different results.
3+
Run `client.php` in your browser, which communicates with `server.php` in the same directory. Experiment by uncommenting the `/*..*/` blocks to send Notify and Batch requests.
4+
5+
Note that all required classes are automatically loaded.

Diff for: example/ServerMethods.php

-45
This file was deleted.
File renamed without changes.

Diff for: example/client.php

+18-39
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
ini_set('default_charset', 'UTF-8');
55
ini_set('display_errors', '1');
66

7-
# autoload for the example directory
8-
require('autoload.php');
7+
# bootstrap for the example directory
8+
require('bootstrap.php');
99

1010
# get the url of the server script
1111
$url = getServerUrl();
@@ -15,70 +15,49 @@
1515

1616
# set up our rpc call with a method and params
1717
$method = 'divide';
18-
$params1 = array(42, 6);
18+
$params = array(42, 6);
1919

20-
$params2 = new stdClass();
21-
$params2->dividend = 42;
22-
$params2->divisor = 6;
20+
$success = false;
2321

24-
$params3 = new stdClass();
25-
$params3->divisor = 6;
26-
$params3->dividend = 42;
27-
28-
$params4 = new stdClass();
29-
$params4->int = true;
30-
$params4->dividend = 23;
31-
$params4->divisor = 6;
32-
33-
//$param1 = 'Hello';
34-
35-
//$param2 = new \stdClass();
36-
//$param2->name = 'Client';
37-
38-
$res = $Client->call($method, $params4);
22+
$success = $Client->call($method, $params);
3923

4024
/*
4125
# notify
42-
$res = $Client->notify($method);
26+
$success = $Client->notify($method);
4327
*/
4428

4529
/*
4630
# batch sending
4731
$Client->batchOpen();
48-
$Client->call($method, array($param1, $param2));
49-
$Client->notify($method, array($param1));
50-
$Client->call($method, array($param1, $param2));
51-
$Client->notify($method, array($param1, $param2));
52-
$Client->call($method, $param2);
53-
$res = $Client->batchSend();
32+
$Client->call($method, $params);
33+
$Client->notify($method, $params);
34+
$Client->call($method, $params);
35+
$Client->notify($method, $params);
36+
$Client->call($method, $params);
37+
$success = $Client->batchSend();
5438
*/
5539

5640
echo '<form method="GET">';
57-
echo '<input type="submit" value="Run Example">';
41+
echo '<input type="submit" value="Run Example"> Last run: ' . date(DATE_RFC822);
5842
echo '</form>';
5943
echo '<pre>';
6044

6145
echo '<b>return:</b> ';
62-
echo $res ? 'true' : 'false';
46+
echo $success ? 'true' : 'false';
6347
echo '<br /><br />';
6448

65-
if (!$res)
49+
if (!$success)
6650
{
67-
echo '<b>fault:</b> ', $Client->fault;
51+
echo '<b>error:</b> ', $Client->error;
6852
echo '<br /><br />';
6953
}
7054

71-
if ($Client->error)
72-
{
73-
echo '<b>error:</b> ', print_r($Client->error, 1);
74-
echo '<br /><br />';
75-
}
76-
elseif ($Client->batch)
55+
if ($Client->batch)
7756
{
7857
echo '<b>batch:</b> ', print_r($Client->batch, 1);
7958
echo '<br /><br />';
8059
}
81-
else
60+
elseif ($Client->result)
8261
{
8362
echo '<b>result:</b> ', print_r($Client->result, 1);
8463
echo '<br /><br />';

Diff for: example/server.php

+31-4
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,46 @@
66
# we don't want any PHP errors being output
77
ini_set('display_errors', '0');
88

9-
# so we will log them. Exception will be logged as well
9+
# so we will log them. Exceptions will be logged as well
1010
ini_set('log_errors', '1');
1111
ini_set('error_log', 'server-errors.log');
1212

13-
# autoload for the example directory
14-
require('autoload.php');
13+
# bootstrap for the example directory
14+
require('bootstrap.php');
1515

1616
# set up our method handler class
17-
require('ServerMethods.php');
1817
$methods = new ServerMethods();
1918

2019
# create our server object, passing it the method handler class
2120
$Server = new JsonRpc\Server($methods);
2221

2322
# and tell the server to do its stuff
2423
$Server->receive();
24+
25+
26+
27+
/**
28+
* Our methods class
29+
*/
30+
class ServerMethods
31+
{
32+
33+
public $error = null;
34+
35+
36+
public function divide($dividend, $divisor, $int = false)
37+
{
38+
39+
if (!$divisor)
40+
{
41+
$this->error = 'Cannot divide by zero';
42+
}
43+
else
44+
{
45+
$quotient = $dividend / $divisor;
46+
return $int ? (int) $quotient : $quotient;
47+
}
48+
49+
}
50+
51+
}

Diff for: example/servertest.php

-55
This file was deleted.

0 commit comments

Comments
 (0)