1
0
mirror of synced 2024-11-27 15:56:06 +03:00
api-client-php/README.md

126 lines
3.4 KiB
Markdown
Raw Permalink Normal View History

2020-11-24 14:23:44 +03:00
[![Build Status](https://github.com/retailcrm/api-client-php/workflows/ci/badge.svg)](https://github.com/retailcrm/api-client-php/actions)
[![Covarage](https://img.shields.io/codecov/c/gh/retailcrm/api-client-php/master.svg?logo=codecov&logoColor=white)](https://codecov.io/gh/retailcrm/api-client-php)
2020-11-24 14:23:44 +03:00
[![Latest stable](https://img.shields.io/packagist/v/retailcrm/api-client-php.svg)](https://packagist.org/packages/retailcrm/api-client-php)
[![PHP from Packagist](https://img.shields.io/packagist/php-v/retailcrm/api-client-php.svg?logo=php&logoColor=white)](https://packagist.org/packages/retailcrm/api-client-php)
2018-02-21 09:42:10 +03:00
2013-07-02 12:37:54 +04:00
# RetailCRM API PHP client
2013-07-03 18:13:25 +04: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
* 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:
```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(
'https://demo.retailcrm.pro',
'T9DMPvuNt7FQJMszHUdG8Fkt6xHsqngH',
2017-06-22 16:42:42 +03:00
\RetailCrm\ApiClient::V5
2014-11-06 02:55:09 +03:00
);
try {
$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(
'https://demo.retailcrm.pro',
'T9DMPvuNt7FQJMszHUdG8Fkt6xHsqngH',
\RetailCrm\ApiClient::V5
2014-11-06 03:18:39 +03:00
);
try {
$response = $client->request->ordersCreate(array(
2014-11-06 03:18:39 +03:00
'externalId' => 'some-shop-order-id',
'firstName' => 'John',
'lastName' => 'Doe',
2014-11-06 03:18:39 +03:00
'items' => array(
//...
),
2014-11-06 03:20:32 +03:00
'delivery' => array(
'code' => 'fedex',
2014-11-06 03:18:39 +03:00
)
));
} 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()) {
echo 'Order successfully created. Order ID into RetailCRM = ' . $response->id;
2016-03-12 01:54:33 +03:00
// 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.pro',
2020-08-21 10:24:55 +03:00
'T9DMPvuNt7FQJMszHUdG8Fkt6xHsqngH',
\RetailCrm\ApiClient::V5
2020-08-21 10:24:55 +03:00
);
$options = new \RetailCrm\Http\RequestOptions(
['X-Rlimit-Token' => 'example_token'], // array of custom headers
10 // client timeout (in seconds)
);
$client->request->setOptions($options);
```