Skip to content

Commit e942706

Browse files
committed
Added docs for listening to events.
1 parent 38e1d51 commit e942706

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,20 @@ Since the version has been explicitly specified using options, the call will be
198198
Since the version has been explicitly specified, the call will be made against version `3`.
199199

200200
$api->v3->user->login(array("login" => "test", "password" => "test"));
201+
202+
## Events
203+
204+
This library emits the following events:
205+
206+
* **success** Emitted when a call succeeds.
207+
* **error** Emitted when a call results in an error.
208+
* **unauthorized** Emitted when a call results in an authentication error. Emitted before emitting the *error* event.
209+
210+
### Listening to events
211+
212+
$api->on('success', function($sender, $call_context, $result){
213+
echo(sprintf("Call to %s.%s resulted in %s", $call_context->service, $call_context->method, json_encode($result)));
214+
});
201215

202216
## Error handling
203217

@@ -235,6 +249,21 @@ Setting `throw_errors` to `false` is more of a way to tell the client to be sile
235249
}
236250
}
237251

252+
### Intercepting errors with events
253+
254+
#### All errors
255+
256+
$api->on('error', function($sender, $call_context, $error){
257+
echo(sprintf("Call to %s.%s resulted in error %s (%s)", $call_context->service, $call_context->method, $error->message, $error->error_code)));
258+
});
259+
260+
#### Only unauthorized events
261+
262+
$api->on('unauthorized', function($sender, $call_context, $error){
263+
header("Location: /login.php");
264+
die();
265+
});
266+
238267
## Solving issues
239268

240269
### See what is being sent to/from UserApp
@@ -285,6 +314,7 @@ Is exactly the same as:
285314

286315
* PHP >= 5.3.2
287316
* [cURL](http://php.net/manual/en/book.curl.php)
317+
* [sabre/event](https://github.com/fruux/sabre-event)
288318

289319
## License
290320

0 commit comments

Comments
 (0)