Interaction with API under hosting restrictions If your site is running on a hosting that does not meet the requirements of the PHP client to interact with API, you can call API using the usual file_get_contents function. In the article, we will analyze examples of its use. false W3sidGl0bGUiOiJBUEkiLCJ1cmwiOiIvZGV2ZWxvcGVycy9hcGkvIn0seyJ0aXRsZSI6IlBlY3VsaWFyaXRpZXMgaW4gd29ya2luZyB3aXRoIEFQSSIsInVybCI6Ii9kZXZlbG9wZXJzL2FwaS9hcGktZmVhdHVyZXMvIn1d API /developers/api/ ["developers","API"] ["developers","API","Peculiarities in working with API"] developers

Interaction with API under hosting restrictions

If your site is running on a hosting that does not meet the requirements of the PHP client to interact with API, you can call API using the usual file_get_contents function. In the article, we will analyze examples of its use.

It is possible that your site is running on a hosting that does not meet the requirements of PHP-client for interacting with API:

  • old PHP version installed (5.2 and below)

  • missing PHP cURL extension

In this case, you can refer to API using the normal function file_get_contents. Below are examples of its use.

Do not use the examples below if your hosting allows you to work with the official API client for PHP. These examples do not include error handling and network problems.

Examples

Creating order

<?php

$crmDomain = 'https://some-crm.retailcrm.pro';
$crmKey = '4325fd34e1kVkahXL8XA3g3DEWIsQnwY';

$postData = http_build_query(array(
 'order' =>json_encode(array(
'firstName' => 'Name',
  'phone' => 'Phone',
  'email' => 'Email',
  'items' => array (
  array (
  'productName' => 'Product name',
  'offer' => array (
  'id' => 'Internal id of the trade offer',
  'externalId' => 'External id of a trade offer',
  'xmlId' => 'id of the trade offer in the warehouse system',
 )),
 array(
 'productName' => 'Product name',
  'offer' => array (
  'id' => 'Internal id of the trade offer',
  'externalId' => 'External id of a trade offer',
  'xmlId' => 'id of the trade offer in the warehouse system',
)))
 '...'
)),
 '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 "ID of the created order = " . $result['id'];

Getting information on order

<?php

$crmDomain = 'https://some-crm.retailcrm.pro';
$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'])) {
 // order information
 print_r($result['order']);
}

Getting order statuses

<?php

$crmDomain = 'https://some-crm.retailcrm.pro';
$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'];
 }
}
Thank you for your feedback.
Was this article helpful?
No
  • The recommendations did not help
  • There is no answer to my question
  • The text is difficult to understand
  • I do not like the described functionality
Yes
Previous article
The procedure of simple connection of the module
The article describes how to implement a simple (one-step) connection of the module
Next article
Procedure for integrating the messenger with the system
Procedure for integrating the messenger with the system