https://github.com/mainwp/mainwp-api-client-php
A PHP wrapper for the MainWP REST API. Easily interact with the MainWP REST API securely using this library. If using a HTTPS connection this library uses BasicAuth, else it uses Bear Token to provide a secure connection to MainWP.
https://github.com/mainwp/mainwp-api-client-php
Last synced: 27 days ago
JSON representation
A PHP wrapper for the MainWP REST API. Easily interact with the MainWP REST API securely using this library. If using a HTTPS connection this library uses BasicAuth, else it uses Bear Token to provide a secure connection to MainWP.
- Host: GitHub
- URL: https://github.com/mainwp/mainwp-api-client-php
- Owner: mainwp
- License: mit
- Created: 2025-02-24T13:03:37.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-07T13:14:40.000Z (over 1 year ago)
- Last Synced: 2025-11-21T18:13:17.206Z (7 months ago)
- Language: PHP
- Homepage: https://mainwp.dev
- Size: 19.5 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MainWP API Client PHP
A PHP wrapper for the MainWP REST API. Easily interact with the MainWP REST API securely using this library. If using a HTTPS connection this library uses BasicAuth, else it uses Bear Token to provide a secure connection to MainWP.
## Installation
Create composer.json as bellow if want to get package from github
```
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/mainwp/mainwp-api-client-php.git"
}
],
"require": {
"mainwp/mainwp-api-client-php": "dev-main"
}
}
```
```
composer require mainwp/mainwp-api-client-php
```
## Getting started
Generate API credentials following this instructions [MainWP REST API](https://mainwp.com/kb/mainwp-rest-api/)
.
Check out the MainWP API endpoints and data that can be manipulated in [MainWP REST API Endpoints](https://www.postman.com/mainwp/mainwp/collection/ujfddk4/mainwp-rest-api-v2-current).
## Setup
Setup for the new WP REST API integration (MainWP 5.2 or later):
```php
require __DIR__ . '/vendor/autoload.php';
use MainWP\Dashboard\Client;
$mainwp = new Client(
'http://example.com',
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
[
'version' => 'v2',
],
);
or
$mainwp = new Client(
'http://example.com',
false,
[
'version' => 'v2',
'version' => 'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'version' => 'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
]
);
```
## Client class
```php
$mainwp = new Client($url, $options, $consumer_apikey );
or
$options['consumer_key'] = $consumer_key;
$options['consumer_secret'] = $consumer_secret;
$mainwp = new Client($url, $options );
```
### Options
| Option | Type | Required | Description |
| ------------------ | -------- | -------- | -------------------------------------------------- |
| `url` | `string` | yes | Your Store URL, example: http://mydashboard.dev/ |
| `consumer_apikey` | `string` | no | Your API consumer Bearer token key |
| `options` | `array` | no | Extra arguments (see client options table) |
#### Client options
| Option | Type | Required | Description |
| ------------------------ | -------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `version` | `string` | no | API version, default is `v2` |
| `timeout` | `int` | no | Request timeout, default is `15` |
| `verify_ssl` | `bool` | no | Verify SSL when connect, use this option as `false` when need to test with self-signed certificates, default is `true` |
| `auth_method` | `string | no | Use Bearer Token `bearer` or Basic 'basic' auth for requests, default is `bearer` | |
| `consumer_key` | `string` | no | Your API consumer key |
| `consumer_secret` | `string` | no | Your API consumer secret |
| `follow_redirects` | `bool` | no | Allow the API call to follow redirects |
| `query_string_auth` | `bool` | no | Force Basic Authentication as query string when `true` and using under HTTPS, default is `false` |
| `user_agent` | `string` | no | Custom user-agent, default is `MainWP API Client-PHP` |
| `method_override_header` | `bool` | no | If true will mask all non-GET/POST methods (PUT/DELETE/etc.) by using POST method with added `X-HTTP-Method-Override: METHOD` HTTP header into request |
| `extension_api` | `string` | no | Use for MainWP's Extensions API |
## Client methods
### GET
```php
$mainwp->get($endpoint, $parameters = []);
```
### POST
```php
$mainwp->post($endpoint, $data);
```
### PUT
```php
$mainwp->put($endpoint, $data);
```
### DELETE
```php
$mainwp->delete($endpoint, $parameters = []);
```
### OPTIONS
```php
$mainwp->options($endpoint);
```
#### Arguments
| Params | Type | Description |
| ------------ | -------- | ------------------------------------------------------------ |
| `endpoint` | `string` | MainWP API endpoint, example: `sites` or `sites/sync/12` |
| `data` | `array` | Only for POST and PUT, data that will be converted to JSON |
| `parameters` | `array` | Only for GET and DELETE, request query string |
#### Response
All methods will return arrays on success or throwing `HttpClientException` errors on failure.
```php
use MainWP\Dashboard\HttpClient\HttpClientException;
try {
// Array of response results.
$results = $mainwp->get('sites');
echo '
' . print_r($results, true) . ''; // JSON output.
// Example: [ 'success' => 1, 'total' => 10, 'data' => [...]]
// Last request data.
$lastRequest = $mainwp->http->getRequest();
echo '' . print_r($lastRequest->getUrl(), true) . ''; // Requested URL (string).
echo '' .
print_r($lastRequest->getMethod(), true) .
''; // Request method (string).
echo '' .
print_r($lastRequest->getParameters(), true) .
''; // Request parameters (array).
echo '' .
print_r($lastRequest->getHeaders(), true) .
''; // Request headers (array).
echo '' . print_r($lastRequest->getBody(), true) . ''; // Request body (JSON).
// Last response data.
$lastResponse = $mainwp->http->getResponse();
echo '
' . print_r($lastResponse->getCode(), true) . ''; // Response code (int).
echo '' .
print_r($lastResponse->getHeaders(), true) .
''; // Response headers (array).
echo '' . print_r($lastResponse->getBody(), true) . ''; // Response body (JSON).
} catch (HttpClientException $e) {
echo '' . print_r($e->getMessage(), true) . ''; // Error message.
echo '' . print_r($e->getRequest(), true) . ''; // Last request data.
echo '' . print_r($e->getResponse(), true) . ''; // Last response data.
}
```
## Release History