This PHP class allows clients to interact with the NWMS API using a simple and straightforward interface. It supports authentication, data submission, and receiving responses from your NWMS system.
Download the NWMSApi.php
file and include it in your project:
<?php
require 'NWMSApi.php';
Create an instance of the client using your login credentials and API base URL:
<?php
$api = new NWMSApi('your_email@example.com', 'your_password', 'https://nwms.cloud/api');
<?php
$api->setRequest([
'sku' => 'TEST-001',
'name' => 'Sample Product',
'quantity' => 10,
])->request('POST', '/products');
$response = $api->getDecodedResult();
print_r($response);
Here's the full source code of the NWMSApi
class:
<?php
class NWMSApi
{
protected string $token = '';
protected string $baseUrl;
protected string $json = '';
protected string $result = '';
public function __construct(string $email, string $password, string $baseUrl = null)
{
$this->baseUrl = rtrim($baseUrl ?? 'https://nwms.cloud/api', '/');
$this->authenticate($email, $password);
}
protected function authenticate(string $email, string $password): void
{
$data = json_encode([
'email' => $email,
'password' => $password,
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->baseUrl . '/login');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Accept: application/json',
]);
$response = curl_exec($ch);
curl_close($ch);
$decoded = json_decode($response, true);
if (isset($decoded['access_token'])) {
$this->token = $decoded['access_token'];
} else {
throw new Exception('Authentication failed: ' . ($decoded['message'] ?? 'Unknown error'));
}
}
public function setRequest(array $data): static
{
$this->json = json_encode($data);
return $this;
}
public function setJsonRequest(string $json): static
{
$this->json = $json;
return $this;
}
public function getRequest(): string
{
return $this->json;
}
public function request(string $method, string $url): static
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
if (!empty($this->json)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->json);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $this->token,
'Content-Type: application/json',
'Accept: application/json',
]);
$this->result = curl_exec($ch);
curl_close($ch);
return $this;
}
public function getResult(): string
{
return $this->result;
}
public function getDecodedResult(): mixed
{
return json_decode($this->result, true);
}
}
If you encounter any issues using the NWMS API client, please contact our support team or refer to the full API documentation.