2018-02-21 09:42:10 +03:00
|
|
|
[![Build Status](https://travis-ci.org/retailcrm/api-client-php.svg?branch=master)](https://travis-ci.org/retailcrm/api-client-php)
|
|
|
|
|
2016-03-12 01:54:33 +03:00
|
|
|
# retailCRM API PHP client
|
2013-07-02 12:37:54 +04:00
|
|
|
|
2017-06-22 00:55:08 +03:00
|
|
|
PHP-client for [retailCRM API](http://www.retailcrm.pro/docs/Developers/ApiVersion5).
|
2013-07-03 18:13:25 +04:00
|
|
|
|
2016-03-12 01:54:33 +03:00
|
|
|
Use [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
|
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
|
|
|
```
|