setApiKey(YOUR_API_KEY);
$rest->setApiSecret(YOUR_API_SECRET);
//$rest->setOffset(100);
$items = $rest->get($url . 'items');
if ($items === false) {
echo 'cUrl error: ' . $rest->getCurlError();
} else {
$code = $rest->getCode();
echo 'Response code: ' . $code. '
';
echo 'Total articles: ' . $rest->getTotal() . '
';
echo 'Showing articles from ' . $rest->getRangeStart() . ' to ' . $rest->getRangeEnd() . '
';
print_r($items);
}
$report = $rest->get($url . 'report/production/products');
if ($report === false) {
echo 'cUrl error: ' . $rest->getCurlError();
} else {
$code = $rest->getCode();
echo 'Response code: ' . $code. '
';
print_r($report);
}
*
* @copyright MRPeasy.com 2024
* @version 1.10.2
*/
class MRPeasyRestClient
{
/**
* Your API key in MRPeasy. You can get it in Settings - API access.
* @var int
*/
private $apiKey;
/**
* Your API secret. You can define it in Settings - API access.
* @var string
*/
private $apiSecret;
/**
* HTTP code of the last response.
* The following options are possible:
* 200 - OK
* 201 - Created - returned if object was created successfully.
* 202 - Accepted - returned if object was updated successfully.
* 204 - No Content - returned if object was deleted successfully.
* 206 - Partial Content - returned if a list of objects was requested and only a part of it (up to 100 objects) was returned.
* 400 - Bad Request - most probably, it is caused by not valid data.
* 401 - Unauthorized - please check API credentials.
* 404 - Not Found - the object requested is not found.
* 429 - Too Many Requests - another request is running at the same time.
* 503 - Service Unavailable - Maintenance. Please try again later.
* @var int
*/
private $responseCode;
/**
* If an array of objects is requested, it will contain not more than 100 objects at one time.
* This variable stores the index of the first returned object in the whole list.
* The first object in the list has index 0.
* You should check it if HTTP code of response is 206.
* @var int
*/
private $rangeStart;
/**
* If an array of objects is requested, it will contain not more than 100 objects at one time.
* This variable stores the index of the last returned object in the whole list.
* You should check it if HTTP code of response is 206.
* @var int
*/
private $rangeEnd;
/**
* If an array of objects is requested, it will contain not more than 100 objects at one time.
* This variable stores the total number of objects in the whole list.
* You should check it if HTTP code of response is 206.
* @var int
*/
private $total;
/**
* If you want to receive next objects from the list, set the desired offset.
* It should be the index of the first object that should be returned.
* @var int
*/
private $offset;
/**
* If you want to receive less than 100 items, you can set the limit.
* It is the total number of items that you want wo receive.
* @var int
*/
private $limit;
/**
* Store cUrl error if any.
*/
private $curlError;
/**
* Set your API key.
* @param string $apiKey
* @return void
*/
public function setApiKey(string $apiKey): void
{
$this->apiKey = $apiKey;
}
/**
* Set your access key.
* @param string $accessKey
* @return void
*/
public function setApiSecret(string $accessKey): void
{
$this->apiSecret = $accessKey;
}
/**
* Get response HTTP code of the last response.
* @return int
*/
public function getCode(): ?int
{
return $this->responseCode;
}
/**
* Get index of the first returned object in the whole list.
* @return int
*/
public function getRangeStart(): ?int
{
return $this->rangeStart;
}
/**
* Get index of the last returned object in the whole list.
* @return int
*/
public function getRangeEnd(): ?int
{
return $this->rangeEnd;
}
/**
* Get total number of objects in the whole list.
* @return int
*/
public function getTotal(): ?int
{
return $this->total;
}
/**
* Set the offset.
* @param int $offset
* @return void
*/
public function setOffset(int $offset): void
{
$this->offset = $offset;
}
/**
* Set the limit.
* @param int $limit
* @return void
*/
public function setLimit(int $limit): void
{
$this->limit = $limit;
}
/**
* Get cUrl error if it has happened.
* @return string
*/
public function getCurlError(): ?string
{
return $this->curlError;
}
/**
* Make a GET request to receive one object or a list of objects.
* If object is not found, returns HTTP code 404.
* @param string $url
* @return mixed
*/
public function get(string $url)
{
$response = $this->call('GET', $url);
if ($response) {
$response = json_decode($response, true);
}
return $response;
}
/**
* Make a POST request to create a new object.
* If data is incorrect, may return HTTP code 400 and, if possible, array of errors.
* @param string $url
* @param array|stdClass $data
* @return int - ID of new object on success, HTTP code 201
* array of errors on fail
*/
public function post(string $url, $data)
{
$response = $this->call('POST', $url, $data);
if ($response) {
$response = json_decode($response, true);
}
return $response;
}
/**
* Make a PUT request to update existing object.
* If data is incorrect, may return HTTP code 400 and, if possible, array of errors.
* If object is not found, may return HTTP code 404.
* @param string $url
* @param array|stdClass $data
* @return void on success, HTTP code must be 202
* array of errors on fail
*/
public function put(string $url, $data)
{
$response = $this->call('PUT', $url, $data);
if ($response) {
$response = json_decode($response, true);
}
return $response;
}
/**
* Make a DELETE request to delete one object.
* It is not allowed to delete all objects by one request.
* @param string $url
* @return void, HTTP code must be 204
*/
public function delete(string $url)
{
$response = $this->call('DELETE', $url);
if ($response) {
$response = json_decode($response, true);
}
return $response;
}
/**
* Perform REST request.
* @param string $method
* @param string $url
* @param array|stdClass|null $data
* @return bool|mixed|string
*/
protected function call(string $method, string $url, $data = null)
{
$this->responseCode = null;
$this->rangeStart = null;
$this->rangeEnd = null;
$this->total = null;
$ch = curl_init();
switch ($method) {
case 'PUT':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
case 'POST':
curl_setopt($ch, CURLOPT_POST, 1);
if ($data) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
break;
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
default:
if ($data) {
$url = $url . '?' . http_build_query($data);
}
}
$headers = [];
$headers[] = 'Authorization: Basic ' . base64_encode($this->apiKey . ':' . $this->apiSecret);
if ($this->offset || $this->limit) {
$start = intval($this->offset);
$range = 'Range: items=' . $start . '-';
if ($this->limit > 0) {
$range .= ($start + $this->limit - 1);
}
$headers[] = $range;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$this, 'header'));
// Uncomment next lines if your cUrl is not configured to validate SSL certificates.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
if ($result === false) {
$error = curl_error($ch);
$this->curlError = $error;
if (strpos($error, 'SSL') !== false) {
$lineNr = __LINE__ - 9;
$this->curlError .= " \nPlease try to uncomment lines " . $lineNr . " and " . ($lineNr + 1) . ".\n";
}
} else {
$this->curlError = null;
}
curl_close($ch);
return $result;
}
/**
* Parse response headers.
* @param $ch
* @param string $header
* @return int
*/
protected function header($ch, string $header): int
{
$header = mb_strtolower($header);
$params = explode(' ', $header);
if (strpos($params[0], 'http/') !== false) {
$this->responseCode = $params[1];
} else if ($params[0] == 'content-range:') {
sscanf($params[2], '%d-%d/%d', $this->rangeStart, $this->rangeEnd, $this->total);
}
return strlen($header);
}
}