{"id":1409,"date":"2022-02-06T23:30:29","date_gmt":"2022-02-06T21:30:29","guid":{"rendered":"https:\/\/wonder-web.com.ua\/?p=1409"},"modified":"2024-04-25T10:46:10","modified_gmt":"2024-04-25T08:46:10","slug":"integratsiya-amocrm-iz-sajtom-api","status":"publish","type":"post","link":"https:\/\/wonder-web.com.ua\/en\/blog\/programming-articles\/integratsiya-amocrm-iz-sajtom-api\/","title":{"rendered":"Integration of amoCRM with API website"},"content":{"rendered":"<p>AmoCRM is one of the most popular CRMs. In this article, we will create a deal in Unsolved, a contact, and a company<\/p>\n<h2>Create integration files<\/h2>\n<p>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<\/p>\n<h2>Getting the keys<\/h2>\n<p>Open AmoCRM, go to Settings > Integrations. Click &#8220;+ Create Integration&#8221; and create an external integration Specify the link to our amo.php file, check the access checkbox and enter an arbitrary name and description:<img loading=\"lazy\" src=\"https:\/\/wonder-web.com.ua\/wp-content\/uploads\/2022\/02\/7ee39d9c6cd1879486626a6e7caddfb3.png\" alt=\"\" width=\"1063\" height=\"654\" \/>We get 3 keys. Open config.php and enter them there:<\/p>\n<pre><code>$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';<\/code><\/pre>\n<p>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:<\/p>\n<pre><code>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, ];\r\n\r\n$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);\r\ncurl_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);\r\n\r\n$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(), ];\r\n\r\n$arrParamsAmo = json_encode($arrParamsAmo); $f = fopen($token_file, 'w'); fwrite($f, $arrParamsAmo); fclose($f); print_r($arrParamsAmo);<\/code><\/pre>\n<p>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<\/p>\n<h2>Updating the token<\/h2>\n<p>The access token is temporary and needs to be updated. Open the access.php file and add the following:<\/p>\n<pre><code>require_once 'config.php'; $dataToken = file_get_contents($token_file); $dataToken = json_decode($dataToken, true);\r\n\r\nif ($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, ];\r\n\r\n    $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);\r\n    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);\r\n\r\n    $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(), ];\r\n\r\n    $arrParamsAmo = json_encode($arrParamsAmo); $f = fopen($token_file, 'w'); fwrite($f, $arrParamsAmo); fclose($f); $access_token = $response['access_token'];\r\n\r\n} else { $access_token = $dataToken[\"access_token\"]; }<\/code><\/pre>\n<p>If you saved the data of the previous database query, write the procedure for retrieving it from the database (lines 59-61).<\/p>\n<h2>Let&#8217;s move on to integration<\/h2>\n<p>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:<\/p>\n<pre><code>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 = '';\r\n\r\n$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\",\r\n                            \"values\" => [ [ \"enum_code\" => \"WORK\", \"value\" => $phone ] ] ] ], [ \"field_id\" => $custom_field_id, \"values\" => [ [ \"value\" => $custom_field_value ] ] ] ] ] ] ], \"companies\" => [ [ \"name\" => $company ] ] ] ],\r\n        \"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 ] ] ],\r\n            [ \"field_code\" => \"UTM_CAMPAIGN\", \"values\" => [ [ \"value\" => $utm_campaign ] ] ], [ \"field_code\" => \"UTM_TERM\", \"values\" => [ [ \"value\" => $utm_term ] ] ], [ \"field_code\" => \"UTM_REFERRER\", \"values\" => [ [ \"value\" => $utm_referrer ] ] ], ], ] ];\r\n\r\n$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));\r\ncurl_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;<\/code><\/pre>\n<p>At the very beginning, we have a list of variables where we need to pass data from the form, for example: $name = $_POST[&#8216;name&#8217;]; In $pipeline_id, you need to write the id of the pipeline, it can be obtained from the crm url: Open the &#8220;Deals&#8221; 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<\/p>\n<h2>Getting field IDs<\/h2>\n<p>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:<\/p>\n<pre><code>require_once 'config.php'; function printLink($method, $title, $subdomain) { echo ' '; echo \"<a href=\"https:\/\/$subdomain.amocrm.ru\/$method\" target=\"_blank\" rel=\"noopener\">$title<\/a>\"; echo ' '; } printLink('api\/v4\/leads\/custom_fields', 'List of utm labels', $subdomain);\r\nprintLink('api\/v4\/users', 'List of users', $subdomain); printLink('api\/v4\/contacts\/custom_fields', 'List of contact fields', $subdomain);<\/code><\/pre>\n<p>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&#8217;s return to the $user_amo variable. Here you need to enter the ID of the user responsible for the transaction. That&#8217;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 \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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..<\/p>\n","protected":false},"author":3,"featured_media":1411,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[34],"tags":[],"_links":{"self":[{"href":"https:\/\/wonder-web.com.ua\/en\/wp-json\/wp\/v2\/posts\/1409"}],"collection":[{"href":"https:\/\/wonder-web.com.ua\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonder-web.com.ua\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonder-web.com.ua\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/wonder-web.com.ua\/en\/wp-json\/wp\/v2\/comments?post=1409"}],"version-history":[{"count":13,"href":"https:\/\/wonder-web.com.ua\/en\/wp-json\/wp\/v2\/posts\/1409\/revisions"}],"predecessor-version":[{"id":2119,"href":"https:\/\/wonder-web.com.ua\/en\/wp-json\/wp\/v2\/posts\/1409\/revisions\/2119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wonder-web.com.ua\/en\/wp-json\/wp\/v2\/media\/1411"}],"wp:attachment":[{"href":"https:\/\/wonder-web.com.ua\/en\/wp-json\/wp\/v2\/media?parent=1409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonder-web.com.ua\/en\/wp-json\/wp\/v2\/categories?post=1409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonder-web.com.ua\/en\/wp-json\/wp\/v2\/tags?post=1409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}