retailCRM Documentation

Interacting with API under restrictions of hosting

The situation can appear when your site works on the hosting, which does not satisfy the requirements PHP-client for interaction with API:

In this case you may refer to API through usual function file_get_contents. Below you may see the examples of this function usage.

 Don`t use these examples if your hosting allows to work with official API-client for PHP https://github.com/retailcrm/api-client-php. These examples do not include the error handling and network  problems.

Examples

Order creating

<?php

$crmDomain = 'https://some-crm.ecomlogic.com';
$crmKey = '4325fd34e1kVkahXL8XA3g3DEWIsQnwY';

$postData = http_build_query(array(
    'order' => json_encode(array(
        'firstName' => 'Name',
        'phone' => 'Phone',
        'email' => 'Email',
    )),
    'apiKey' => $crmKey,
));

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postData
    )
);

$context  = stream_context_create($opts);
$result = json_decode(
    file_get_contents(
        $crmDomain . '/api/v4/orders/create', 
        false, 
        $context
    ),
    true
);

echo "created order ID = " . $result['id'];

Getting information on order

<?php

$crmDomain = 'https://some-crm.ecomlogic.com';
$crmKey = '4325fd34e1kVkahXL8XA3g3DEWIsQnwY';

// example
$orderId = 5;
$params = array(
    'by' => 'id',
    'apiKey' => $crmKey,
);

$result = json_decode(
    file_get_contents($crmDomain . '/api/v4/orders/' . $orderId . '?' . http_build_query($params)),
    true
);

if (isset($result['order'])) {
    // information on order
    print_r($result['order']);
}

Getting of order statuses

<?php

$crmDomain = 'https://some-crm.ecomlogic.com';
$crmKey = '4325fd34e1kVkahXL8XA3g3DEWIsQnwY';

// example
$orderIds = array(1, 2, 3);
$params = array(
    'ids' => $orderIds,
    'apiKey' => $crmKey,
);

$result = json_decode(
    file_get_contents($crmDomain . '/api/v4/orders/statuses' . http_build_query($params)),
    true
);

if (isset($result['orders'])) {
    foreach ($result['orders'] as $order) {
        echo $order['id'];
        echo $order['status'];
    }
}

PrintEditHistory
Page last modified on October 20, 2017, at 05:01 PM