|
1 |
| -## cPanel UAPI and API2 PHP class |
| 1 | +cPanel UAPI and API2 PHP class |
| 2 | +=== |
| 3 | +[](https://github.com/N1ghteyes/cpanel-UAPI-php-class/releases) |
2 | 4 |
|
3 |
| -PHP class to provide an easy to use interface with cPanel’s UAPI and API2. |
4 |
| -Uses php magic functions to provide a simple and powerful interface. |
| 5 | +PHP class to provide an easy-to-use interface with cPanel's UAPI and API2. |
| 6 | +Uses PHP magic functions to provide a simple and powerful interface. |
5 | 7 |
|
6 |
| -v2.0 is not backwards compatible, and will likley under go a few more changes, see the changelog for details. |
7 |
| -The class has been renamed to cpanelAPI. |
| 8 | +v2.0 is not backwards compatible with v1.x, and will likley undergo a few more changes. See the [changelog](changelog.txt) for details. |
| 9 | +The class has been renamed to `cpanelAPI`. |
8 | 10 | Some more testing is required.
|
9 | 11 |
|
10 | 12 | ## Usage
|
11 | 13 |
|
12 |
| -See the example files, but typical useage takes the form of: |
| 14 | +See the example files, but typical usage takes the form of: |
13 | 15 |
|
14 | 16 | ### Instantiate the class
|
15 |
| -``` |
16 |
| -$capi = new cpanelAPI('user', 'password', 'cpanel.example.com'); |
| 17 | +```php |
| 18 | +$cPanel = new cpanelAPI('user', 'password', 'cpanel.example.com'); |
17 | 19 | ```
|
18 | 20 | The API we want to use and the Module (also called Scope) are now protected and are set by `__get()`.
|
19 |
| -The request layout looks like this: `$capi->api->Module->request(args[])` |
20 | 21 |
|
21 |
| -For example. We want to use the UAPI to call the Mysql get_restrictions function. |
22 |
| -``` |
23 |
| -$response = $capi->uapi->Mysql->get_restrictions(); |
24 |
| -print_r($response); |
25 |
| -``` |
| 22 | +The request layout looks like this: `$cPanel->api->method->Module->request(args[])` |
| 23 | + |
| 24 | +The `->method` part should be replaced with `->get` for GET requests and `->post` for POST requests, or omitted to default to GET requests. |
26 | 25 |
|
27 |
| -Now that we have set both the api AND the Module, we can call other functions within this api and scope without specifying them again |
28 |
| -We have database prefixing enabled so we have to pass the usename into this function |
29 |
| -see https://documentation.cpanel.net/display/SDK/UAPI+Functions+-+Mysql%3A%3Acreate_database |
| 26 | +As an example, suppose we want to use the UAPI to call the [Mysql::get_server_information](https://documentation.cpanel.net/display/SDK/UAPI+Functions+-+Mysql%3A%3Aget_server_information) function: |
| 27 | + |
| 28 | +```php |
| 29 | +$response = $cPanel->uapi->Mysql->get_server_information(); |
| 30 | +var_dump($response); |
30 | 31 | ```
|
31 |
| -$response = $capi->create_database(['name' => $capi->user.'_MyDatabase']); |
32 |
| -print_r($response); |
| 32 | + |
| 33 | +Now that we have set both the API *and* the Module, we can call other functions within this API and Module without specifying them again: |
| 34 | + |
| 35 | +```php |
| 36 | +$response = $cPanel->create_database(['name' => $cPanel->user.'_MyDatabase']); |
| 37 | +var_dump($response); |
33 | 38 | ```
|
34 | 39 |
|
35 |
| -we can also change the scope without respecifying the api, note that the Module call is case-sensitive. |
| 40 | +We can also change the Module scope without respecifying the API. Note that the Module call is case-sensitive. |
| 41 | + |
| 42 | +```php |
| 43 | +$response = $cPanel->SSL->list_certs(); |
36 | 44 | ```
|
37 |
| -$response = $capi->SSL->list_certs(); |
| 45 | + |
| 46 | +#### File upload example |
| 47 | + |
| 48 | +```php |
| 49 | +$cPanel = new cpanelAPI($username, $password, $hostname); |
| 50 | +$cPanel->uapi->post->Fileman |
| 51 | + ->upload_files(['dir' => REMOTE_PATH_RELATIVE_TO_HOME, |
| 52 | + 'file-1' => new CURLFile(LOCAL_PATH_TO_FILE) |
| 53 | + ]); |
38 | 54 | ```
|
39 | 55 |
|
40 | 56 | ### API2
|
41 | 57 |
|
42 | 58 | API2 is used in exactly the same way as the UAPI
|
43 |
| -``` |
44 |
| -$cpapi2 = new cpanelAPI('user', 'password', 'cpanel.example.com'); |
45 |
| -``` |
46 | 59 |
|
47 |
| -For example. We want to use the API2 to add a subdomain |
48 |
| -``` |
49 |
| -$response = $capi->api2->SubDomain->addsubdomain(['rootdomain' => 'domain.com', 'domain' => 'sub']); |
50 |
| -print_r($response); |
| 60 | +```php |
| 61 | +$cPanel = new cpanelAPI('user', 'password', 'cpanel.example.com'); |
51 | 62 | ```
|
52 | 63 |
|
53 |
| -### 2 Factor Authentication |
| 64 | +For example, suppose we want to use the API2 to add a subdomain: |
54 | 65 |
|
55 |
| -To use this class on a cPanel instance with 2 Factor authentication, you need to pass the secret into the class constructor. |
| 66 | +```php |
| 67 | +$response = $cPanel->api2->SubDomain->addsubdomain(['rootdomain' => 'domain.com', 'domain' => 'sub']); |
| 68 | +var_dump($response); |
56 | 69 | ```
|
57 |
| -$capi = new cpanelAPI('user', 'password', 'cpanel.example.com', 'secret'); |
| 70 | + |
| 71 | +### Two-Factor Authentication |
| 72 | + |
| 73 | +To use this class on a cPanel instance with two-factor authentication (2FA), you need to pass the secret into the class constructor: |
| 74 | + |
| 75 | +```php |
| 76 | +$cPanel = new cpanelAPI('user', 'password', 'cpanel.example.com', 'secret'); |
58 | 77 | ```
|
59 | 78 |
|
60 |
| -The secret can be found on the 2fa setup page. See: https://documentation.cpanel.net/display/ALD/Two-Factor+Authentication+for+cPanel#Two-FactorAuthenticationforcPanel-Configure2FA |
| 79 | +The secret can be found on the 2FA setup page. See [Two-Factor Authentication for cPanel – Configure two-factor authentication](https://documentation.cpanel.net/display/ALD/Two-Factor+Authentication+for+cPanel#Two-FactorAuthenticationforcPanel-Configure2FA) for details. |
0 commit comments