Integration of amoCRM with the site API - instructions | WonderWeb digital
Wonder Web
leave
a request
menu
UA EN RU

Integration of amoCRM with API website

AmoCRM is one of the most popular CRMs. In this article, we will create a deal in Unsolved, a contact, and a company

Create integration files

Create an amo folder, for example, in the root of the site. Files inside: amo.php access.php config.php get.php cookie.txt tokens.txt The names can be arbitrary

Getting the keys

Open AmoCRM, go to Settings > Integrations. Click “+ Create Integration” and create an external integration Specify the link to our amo.php file, check the access checkbox and enter an arbitrary name and description:We get 3 keys. Open config.php and enter them there:

$subdomain = ''; // AmoCRM subdomain $client_secret = ''; // Secret key $client_id = ''; // Integration ID $code = ''; // Authorization code $token_file = 'tokens.txt'; $redirect_uri = 'https://site.com/amo/amo.php';

We take the AmoCRM subdomain from the url of our CRM In $redirect_uri, do not forget to change your site domain Then go to the auth.php file and insert the following:

require_once 'config.php'; $link = "https://$subdomain.amocrm.ru/oauth2/access_token"; $data = [ 'client_id' => $client_id, 'client_secret' => $client_secret, 'grant_type' => 'authorization_code', 'code' => $code, 'redirect_uri' => $redirect_uri, ];

$curl = curl_init(); curl_setopt($curl,CURLOPT_RETURNTRANSFER, true); curl_setopt($curl,CURLOPT_USERAGENT,'amoCRM-oAuth-client/1.0'); curl_setopt($curl,CURLOPT_URL, $link); curl_setopt($curl,CURLOPT_HTTPHEADER, ['Content-Type:application/json']); curl_setopt($curl,CURLOPT_HEADER, false); curl_setopt($curl,CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl,CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, 2); $out = curl_exec($curl); $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); $code = (int)$code; $errors = [ 301 => 'Moved permanently.', 400 => 'Wrong structure of the array of transmitted data, or invalid identifiers of custom fields.', 401 => 'Not Authorized. There is no account information on the server. You need to make a request to another server on the transmitted IP.', 403 => 'The account is blocked, for repeatedly exceeding the number of requests per second.', 404 => 'Not found.', 500 => 'Internal server error.', 502 => 'Bad gateway.', 503 => 'Service unavailable.' ]; if ($code < 200 || $code > 204) die("Error $code. " . (isset($errors[$code]) ? $errors[$code] : 'Undefined error') ); $response = json_decode($out, true);

$arrParamsAmo = [ "access_token" => $response['access_token'], "refresh_token" => $response['refresh_token'], "token_type" => $response['token_type'], "expires_in" => $response['expires_in'], "endTokenTime" => $response['expires_in'] + time(), ];

$arrParamsAmo = json_encode($arrParamsAmo); $f = fopen($token_file, 'w'); fwrite($f, $arrParamsAmo); fclose($f); print_r($arrParamsAmo);

Here we send a request to AmoCRM to get the tokens. The tokens are saved to a file (45-57 lines), you can store them in the database. Run the file https://site.com/amo/auth.php As a result, the data should appear in the tokens.txt file

Updating the token

The access token is temporary and needs to be updated. Open the access.php file and add the following:

require_once 'config.php'; $dataToken = file_get_contents($token_file); $dataToken = json_decode($dataToken, true);

if ($dataToken["endTokenTime"] < time()) { $link = "https://$subdomain.amocrm.ru/oauth2/access_token"; $data = [ 'client_id' => $client_id, 'client_secret' => $client_secret, 'grant_type' => 'refresh_token', 'refresh_token' => $dataToken["refresh_token"], 'redirect_uri' => $redirect_uri, ];

    $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_USERAGENT, 'amoCRM-oAuth-client/1.0'); curl_setopt($curl, CURLOPT_URL, $link); curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type:application/json']); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); $out = curl_exec($curl); $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); $code = (int)$code; $errors = [ 301 => 'Moved permanently.', 400 => 'Wrong structure of the array of transmitted data, or invalid identifiers of custom fields.', 401 => 'Not Authorized. There is no account information on the server. You need to make a request to another server on the transmitted IP.', 403 => 'The account is blocked, for repeatedly exceeding the number of requests per second.', 404 => 'Not found.', 500 => 'Internal server error.', 502 => 'Bad gateway.', 503 => 'Service unavailable.' ]; if ($code < 200 || $code > 204) die("Error $code. " . (isset($errors[$code]) ? $errors[$code] : 'Undefined error') ); $response = json_decode($out, true);

    $arrParamsAmo = [ "access_token" => $response['access_token'], "refresh_token" => $response['refresh_token'], "token_type" => $response['token_type'], "expires_in" => $response['expires_in'], "endTokenTime" => $response['expires_in'] + time(), ];

    $arrParamsAmo = json_encode($arrParamsAmo); $f = fopen($token_file, 'w'); fwrite($f, $arrParamsAmo); fclose($f); $access_token = $response['access_token'];

} else { $access_token = $dataToken["access_token"]; }

If you saved the data of the previous database query, write the procedure for retrieving it from the database (lines 59-61).

Let’s move on to integration

I will not give an example of an html form, you need to process the form and pass the necessary data to amo.php. Open amo.php and add:

require_once 'access.php'; $name = 'Customer name'; $phone = '+380123456789'; $email = 'email@gmail.com'; $target = 'Target'; $company = 'Company name'; $custom_field_id = 454021; $custom_field_value = 'test'; $ip = '1.2.3.4'; $domain = 'site.com'; $price = 0; $pipeline_id = 5059931; $user_amo = 0; $utm_source = ''; $utm_content = ''; $utm_medium = ''; $utm_campaign = ''; $utm_term = ''; $utm_referrer = '';

$data = [ [ "name" => $phone, "price" => $price, "responsible_user_id" => $user_amo, "pipeline_id" => $pipeline_id, "_embedded" => [ "metadata" => [ "category" => "forms", "form_id" => 1, "form_name" => "Form on site", "form_page" => $target, "form_sent_at" => strtotime(date("Y-m-d H:i:s")), "ip" => $ip, "referer" => $domain ], "contacts" => [ [ ["first_name" => $name, "custom_fields_values" => [ [ "field_code" => "EMAIL", "values" => [ [ "enum_code" => "WORK", "value" => $email ] ] ], [ "field_code" => "PHONE",
                            "values" => [ [ "enum_code" => "WORK", "value" => $phone ] ] ] ], [ "field_id" => $custom_field_id, "values" => [ [ "value" => $custom_field_value ] ] ] ] ] ] ], "companies" => [ [ "name" => $company ] ] ] ],
        "custom_fields_values" => [ [ "field_code" => "UTM_SOURCE", "values" => [ [ "value" => $utm_source ] ] ], [ "field_code" => "UTM_CONTENT", "values" => [ [ "value" => $utm_content ] ] ], [ "field_code" => "UTM_MEDIUM", "values" => [ [ "value" => $utm_medium ] ] ],
            [ "field_code" => "UTM_CAMPAIGN", "values" => [ [ "value" => $utm_campaign ] ] ], [ "field_code" => "UTM_TERM", "values" => [ [ "value" => $utm_term ] ] ], [ "field_code" => "UTM_REFERRER", "values" => [ [ "value" => $utm_referrer ] ] ], ], ] ];

$method = "/api/v4/leads/complex"; $headers = [ 'Content-Type: application/json', 'Authorization: Bearer ' . $access_token, ]; $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_USERAGENT, 'amoCRM-API-client/1.0'); curl_setopt($curl, CURLOPT_URL, "https://$subdomain.amocrm.ru".$method); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_COOKIEFILE, 'amo/cookie.txt'); curl_setopt($curl, CURLOPT_COOKIEJAR, 'amo/cookie.txt'); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); $out = curl_exec($curl); $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); $code = (int) $code; $errors = [ 301 => 'Moved permanently.', 400 => 'Wrong structure of the array of transmitted data, or invalid identifiers of custom fields.', 401 => 'Not Authorized. There is no account information on the server. You need to make a request to another server on the transmitted IP.', 403 => 'The account is blocked, for repeatedly exceeding the number of requests per second.', 404 => 'Not found.', 500 => 'Internal server error.', 502 => 'Bad gateway.', 503 => 'Service unavailable.' ]; if ($code < 200 || $code > 204) die("Error $code. " . (isset($errors[$code]) ? $errors[$code] : 'Undefined error') ); $Response = json_decode($out, true); $Response = $Response['_embedded']['items']; $output = 'ID of added list items:' . PHP_EOL; foreach ($Response as $v) if (is_array($v)) $output .= $v['id'] . PHP_EOL; return $output;

At the very beginning, we have a list of variables where we need to pass data from the form, for example: $name = $_POST[‘name’]; In $pipeline_id, you need to write the id of the pipeline, it can be obtained from the crm url: Open the “Deals” section, take the id of the opened pipeline (the number at the end of the url), or switch to another one. We will return to $user_amo a little later. Fill in the rest of the variables. Fill in the $data array with information at your discretion, according to the documentation

Getting field IDs

Getting field, user, and other identifiers is implemented through GET requests. More details can be found in the documentation, but for our purposes, I have prepared a separate file that needs this data. Open the get.php file and add to it:

require_once 'config.php'; function printLink($method, $title, $subdomain) { echo ' '; echo "$title"; echo ' '; } printLink('api/v4/leads/custom_fields', 'List of utm labels', $subdomain);
printLink('api/v4/users', 'List of users', $subdomain); printLink('api/v4/contacts/custom_fields', 'List of contact fields', $subdomain);

Run the file: https://site.com/amo/get.php You will see a list of links, clicking on which you get a list of id and other field parameters for utm labels, users, contacts. By analogy, you can view the rest. Let’s return to the $user_amo variable. Here you need to enter the ID of the user responsible for the transaction. That’s all, the leads will come to the unprocessed, create a contact, pull up the name, phone number, as well as custom fields, utm labels that were specified in $data and the company name. If you need help integrating CRM systems, write to us 😉

Author Innocentiy Luzhnov

Creative content manager, “WonderWeb”

like?
Do you have a project?

let's discuss it, think it over and do it!