2018-02-21 11:06:29 +03:00
[![Build Status ](https://img.shields.io/travis/retailcrm/api-client-php/master.svg?style=flat-square )](https://travis-ci.org/retailcrm/api-client-php)
2019-08-30 14:10:52 +03:00
[![Covarage ](https://img.shields.io/codecov/c/gh/retailcrm/api-client-php/master.svg?style=flat-square )](https://codecov.io/gh/retailcrm/api-client-php)
2018-02-21 11:06:29 +03:00
[![Latest stable ](https://img.shields.io/packagist/v/retailcrm/api-client-php.svg?style=flat-square )](https://packagist.org/packages/retailcrm/api-client-php)
2018-03-20 21:28:14 +03:00
[![PHP from Packagist ](https://img.shields.io/packagist/php-v/retailcrm/api-client-php.svg?style=flat-square )](https://packagist.org/packages/retailcrm/api-client-php)
2018-02-21 09:42:10 +03:00
2013-07-02 12:37:54 +04:00
2018-03-20 21:28:14 +03:00
# retailCRM API PHP client
2013-07-03 18:13:25 +04:00
2018-03-20 21:28:14 +03:00
This is php retailCRM API client. This library allows to use all available API versions. [API documentation ](http://retailcrm.github.io/api-client-php )
2014-11-13 13:04:11 +03:00
2016-03-12 01:54:33 +03:00
## Requirements
2013-07-03 18:13:25 +04:00
2017-06-22 00:55:08 +03:00
* PHP 5.4 and above
2016-03-12 01:54:33 +03:00
* PHP's cURL support
2019-08-30 14:10:52 +03:00
* PHP's JSON support
* PHP's Fileinfo support
2013-07-04 12:44:41 +04:00
2016-03-12 01:54:33 +03:00
## Install
2013-07-04 12:44:41 +04:00
2016-03-12 01:54:33 +03:00
1) Get [composer ](https://getcomposer.org/download/ )
2013-07-04 12:44:41 +04:00
2016-03-12 01:54:33 +03:00
2) Run into your project directory:
2014-11-06 02:44:52 +03:00
```bash
2017-06-22 16:42:42 +03:00
composer require retailcrm/api-client-php ~5.0
2013-10-08 11:19:38 +04:00
```
2014-11-06 02:55:09 +03:00
2016-03-12 01:54:33 +03:00
If you have not used `composer` before, include autoloader into your project.
2014-11-07 13:05:40 +03:00
```php
require 'path/to/vendor/autoload.php';
```
2016-03-12 01:54:33 +03:00
## Usage
2014-11-06 02:55:09 +03:00
2016-03-12 01:54:33 +03:00
### Get order
2014-11-07 11:31:33 +03:00
```php
2014-11-06 02:55:09 +03:00
$client = new \RetailCrm\ApiClient(
2017-06-22 00:55:08 +03:00
'https://demo.retailcrm.ru',
'T9DMPvuNt7FQJMszHUdG8Fkt6xHsqngH',
2017-06-22 16:42:42 +03:00
\RetailCrm\ApiClient::V5
2014-11-06 02:55:09 +03:00
);
try {
2017-06-22 00:55:08 +03:00
$response = $client->request->ordersGet('M-2342');
2014-11-06 02:55:09 +03:00
} catch (\RetailCrm\Exception\CurlException $e) {
2016-03-12 01:54:33 +03:00
echo "Connection error: " . $e->getMessage();
2014-11-06 02:55:09 +03:00
}
if ($response->isSuccessful()) {
echo $response->order['totalSumm'];
2016-03-12 01:54:33 +03:00
// or $response['order']['totalSumm'];
// or
2014-11-06 02:55:09 +03:00
// $order = $response->getOrder();
// $order['totalSumm'];
2014-11-06 11:31:47 +03:00
} else {
echo sprintf(
2016-03-12 01:54:33 +03:00
"Error: [HTTP-code %s] %s",
2014-11-06 11:31:47 +03:00
$response->getStatusCode(),
$response->getErrorMsg()
);
2016-03-09 02:34:13 +03:00
2016-03-12 01:54:33 +03:00
// error details
2014-11-07 11:34:54 +03:00
//if (isset($response['errors'])) {
// print_r($response['errors']);
//}
2014-11-06 02:55:09 +03:00
}
2014-11-06 03:18:39 +03:00
```
2016-03-12 01:54:33 +03:00
### Create order
2014-11-06 03:18:39 +03:00
```php
$client = new \RetailCrm\ApiClient(
2017-06-22 00:55:08 +03:00
'https://demo.retailcrm.ru',
'T9DMPvuNt7FQJMszHUdG8Fkt6xHsqngH',
2017-06-22 16:42:42 +03:00
\RetailCrm\ApiClient::V4
2014-11-06 03:18:39 +03:00
);
try {
2017-06-22 00:55:08 +03:00
$response = $client->request->ordersCreate(array(
2014-11-06 03:18:39 +03:00
'externalId' => 'some-shop-order-id',
'firstName' => 'Vasily',
'lastName' => 'Pupkin',
'items' => array(
//...
),
2014-11-06 03:20:32 +03:00
'delivery' => array(
2014-11-06 03:18:39 +03:00
'code' => 'russian-post',
)
));
} catch (\RetailCrm\Exception\CurlException $e) {
2016-03-12 01:54:33 +03:00
echo "Connection error: " . $e->getMessage();
2014-11-06 03:18:39 +03:00
}
2014-11-06 19:15:51 +03:00
if ($response->isSuccessful() & & 201 === $response->getStatusCode()) {
2016-03-12 01:54:33 +03:00
echo 'Order successfully created. Order ID into retailCRM = ' . $response->id;
// or $response['id'];
// or $response->getId();
2014-11-06 11:31:47 +03:00
} else {
echo sprintf(
2016-03-12 01:54:33 +03:00
"Error: [HTTP-code %s] %s",
2014-11-06 11:31:47 +03:00
$response->getStatusCode(),
$response->getErrorMsg()
);
2014-11-07 11:34:54 +03:00
2016-03-12 01:54:33 +03:00
// error details
2014-11-07 11:34:54 +03:00
//if (isset($response['errors'])) {
// print_r($response['errors']);
//}
2014-11-06 03:18:39 +03:00
}
2014-11-06 17:24:44 +03:00
```
2018-03-20 21:28:14 +03:00
2020-08-21 10:24:55 +03:00
### Set custom headers and client timeout
```php
$client = new \RetailCrm\ApiClient(
'https://demo.retailcrm.ru',
'T9DMPvuNt7FQJMszHUdG8Fkt6xHsqngH',
\RetailCrm\ApiClient::V4
);
$options = new \RetailCrm\Http\RequestOptions(
['X-Rlimit-Token' => 'example_token'], // array of custom headers
10 // client timeout (in seconds)
);
$client->request->setOptions($options);
```
2018-03-20 21:28:14 +03:00
### Documentation
2019-08-30 14:10:52 +03:00
* [English ](https://help.retailcrm.pro/Developers )
* [Russian ](https://help.retailcrm.ru/Developers )