Skip to content

Commit 686795b

Browse files
committed
Update readme and comments
1 parent 6faee70 commit 686795b

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

README.md

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@ Created by [Hieu Le](http://www.hieule.info)
99

1010
MIT licensed.
1111

12-
Current version: 2.3.0
12+
Current version: 2.4.0
1313

1414

1515
## Features
16+
1617
* Full test suit built in supporting testing using your own Wordpress site.
17-
* Support error logging to files with Monolog library.
18+
* ~~Support error logging to files with Monolog library.~~ Now, erros can be logged in a more felxible way via **error callbacks** (v 2.4.0)
1819
* Support UTF-8 content.
1920
* Closely implement the whole [XML-RPC WordPress API](http://codex.Wordpress.org/XML-RPC_WordPress_API).
2021
* Detail exception will be thrown when errors occurs.
2122
* (v2.2) Support proxy and http authentication.
2223
* (v2.2.1) Allow value of `DateTime` class to be convert correctly to `datetime.iso8601` XML-RPC type,
24+
* (v2.4.0) Support using custom User Agent string beside the default User Agent string.
25+
* (v2.4.0) Support callbacks on **sending** and **error** events
2326

2427
## Installation
2528

26-
You will need [Composer](https://getcomposer.org/) installed on your machine to use this library. Verify that composer is installed by typing this command
29+
~~You will need [Composer](https://getcomposer.org/) installed on your machine to use this library~~ [Composer](https://getcomposer.org/) now is not required but recommended. Verify that composer is installed by typing this command
2730

2831
```bash
2932
composer --version
@@ -32,6 +35,7 @@ composer --version
3235
Choose one of the following methods to install **Wordpress XML-RPC PHP Client**
3336

3437
### Your project has used composer:
38+
3539
Add this dependency into your `composer.json` file
3640

3741
```json
@@ -41,34 +45,37 @@ Add this dependency into your `composer.json` file
4145
After that, run `composer update` to install this package.
4246

4347
### Your project does not use composer:
44-
Clone or download the archive of this package from [github](https://github.com/letrunghieu/Wordpress-xmlrpc-client/releases). Copy the package directory into a location of your project. Open the command line terminal and do these command
4548

46-
```bash
47-
cd library/installed/dir
48-
composer install
49-
```
49+
Clone or download the archive of this package from [github](https://github.com/letrunghieu/Wordpress-xmlrpc-client/releases). Include all files in the `src` directory into your project and start using **Wordpress XML-RPC Client**. You have to update the code of this library manually if using it without Composer.
5050

51-
After the installation progress finished, there will be a file called `autoload.php` created inside the `vendor` sub folder of the library. You should include this file to use **Wordpress XML-RPC PHP Client**.
51+
Required PHP extension is `xmlrpc` extension. The `curl` extension is optional be recommended.
5252

5353

5454
## Usage
5555

56-
All API call will be executed via an instance of the `WordpressClient` class. This is the way we initiate it:
56+
All API call will be executed via an instance of the `WordpressClient` class. Since version 2.4.0, there is no mandatory parameters in the contructor. `endPoint`, `username`, and `password` can be updated anytime by calling `setCredentials` method. The last parameter in previous version contructor (which is an instance of `\Illuminate\Log\Writer` class) is deprecated and will be removed in the next major release. Below is an example of using this library:
5757

5858
```php
5959
# Your Wordpress website is at: http://wp-website.com
6060
$endpoint = "http://wp-website.com/xmlrpc.php";
6161

62-
# The logger instance
63-
$wpLog = new \Illuminate\Log\Writer(new Monolog\Logger('wp-xmlrpc'));
64-
65-
# Save logs into file
66-
$wpLog->useFiles('path-to-your-log-file');
62+
# The Monolog logger instance
63+
$wpLog = new Monolog\Logger('wp-xmlrpc');
6764

6865
# Create client instance
69-
# The logger instance is optional
70-
$wpClient = new \HieuLe\WordpressXmlrpcClient\WordpressClient($endpoint, 'username', 'password', $wpLog);
66+
$wpClient = new \HieuLe\WordpressXmlrpcClient\WordpressClient();
67+
# Log error
68+
$wpClient->onError(function($error, $event) use ($wpLog){
69+
$wpLog->addError($error, $event);
70+
});
71+
72+
# Set the credentials for the next requests
73+
$wpClient->setCredentials($endpoint, 'username', 'password');
74+
7175
```
76+
77+
If you have used logging feture of previous version of this library, you should update your code to use the new way of loggin as above, the Monolog instance can be replaced by any kinds of logging tool that you have.
78+
7279
To use date time value, you must use an instance of `DateTime` class instead of a string.
7380

7481
There will be 2 types of exception may be thrown from this library:
@@ -78,7 +85,41 @@ There will be 2 types of exception may be thrown from this library:
7885

7986
For API reference, visit [Wordpress documentation](http://codex.Wordpress.org/XML-RPC_WordPress_API) or [Library API documentation](http://letrunghieu.github.io/wordpress-xmlrpc-client/api/index.html)
8087

88+
## User Agent (since 2.4.0)
89+
90+
The library use the default User Agent when contacting with Wordpress blogs. If you want to use onother one, pass your custom User Agent string into the `setUserAgent` method. If you passed a _falsy_ value (`null`, `false`, ...) the default one will be used (thank @WarrenMoore)
91+
92+
## Callbacks and events (since 2.4.0)
93+
94+
The library allow developers to listen on two events `Sending` and `Error`. You can add new closure as a callback for each events by calling `on<event>` method with the closure as parameter (see the `onError` example above).
95+
96+
### `onSending($event)`
97+
98+
This event is fired before each request is send to Wordpress blogs. `$event` is an array:
99+
100+
- `event`: the name of the event, here is `sending`
101+
- `endpoint`: URL of the current endpoint
102+
- `username`: current username
103+
- `password`: current password
104+
- `method`: current XML-RPC method
105+
- `params`: parameters passed to the current method
106+
- `request`: the body of the current request which will be sent
107+
- `proxy`: current proxy config
108+
- `auth`: current http auth config
109+
110+
### `onError($errorMessage, $event)`
111+
112+
This event is fired when the library run into errors, before any exception thrown. `$errorMessage` is a string. `$event` is an array:
113+
114+
- `event`: the name of the event, here is `sending`
115+
- `endpoint`: URL of the current endpoint
116+
- `request`: the body of the current request
117+
- `proxy`: current proxy config
118+
- `auth`: current http auth config
119+
120+
81121
## Unit testing
122+
82123
By default, the project use recorded data as the default data for test suite. However, if you want to test with your own Wordpress installation, there are available options inside the `./tests/xmlrpc.yml` file:
83124

84125
* `endpoint`: the url of your Wordpress XML-RPC endpoint

src/WordpressClient.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class WordpressClient
1818
private $_password;
1919
private $_endPoint;
2020
private $_request;
21-
private $_response;
2221
private $_responseHeader = array();
2322
private $_error;
2423
private $_proxyConfig = false;
@@ -121,6 +120,8 @@ function setUserAgent($userAgent)
121120
* Get current user agent string
122121
*
123122
* @return string
123+
*
124+
* @since 2.4.0
124125
*/
125126
function getUserAgent()
126127
{

0 commit comments

Comments
 (0)