A
A
Andrey Igorevich2021-01-13 08:06:32
opencart
Andrey Igorevich, 2021-01-13 08:06:32

How to integrate amocrm with php site?

Good afternoon, tell me who may have already done this, I'm trying to connect to AMOCRM on the site using PHP code:

$subdomain = " /*Наш поддомен в AMOCRM*/ ";
    $link = 'https://' . $subdomain . '.amocrm.ru/oauth2/access_token';
    $data = [
       'client_id' => ' /*id созданной нами интеграции в crm*/ ',
       'client_secret' => ' /*секретный ключ оттуда же*/ ',
       'grand_type' => 'autorization_code',
       'code' => ' /*Код авторизации оттуда же*/ ',
       'redirect_uri' => ' /* Сайт, на котором установлен этот код, с доменом 3-го уровня, без ssl сертификата */'
    ];

    $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); //Инициируем запрос к API и сохраняем ответ в переменную
    $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);

    $code = (int)$code;
    $errors = [
      400 => 'Bad request',
      401 => 'Unauthorized',
      403 => 'Forbidden',
      404 => 'Not found',
      500 => 'Internal server error',
      502 => 'Bad gateway',
      503 => 'Service unavailable',
    ];

    try
    {
      /** Если код ответа не успешный - возвращаем сообщение об ошибке  */
      if ($code < 200 || $code > 204) {
        throw new Exception(isset($errors[$code]) ? $errors[$code] : 'Undefined error', $code);
      }
    }
    catch(\Exception $e)
    {
      die('Ошибка: ' . $e->getMessage() . PHP_EOL . 'Код ошибки: ' . $e->getCode());
    }

    $response = json_decode($out, true);

    $access_token = $response['access_token']; //Access токен
    $refresh_token = $response['refresh_token']; //Refresh токен
    $token_type = $response['token_type']; //Тип токена
    $expires_in = $response['expires_in']; //Через сколько действие токена истекает


All code from the documentation, only our values ​​are substituted. The site is absolutely new and bare on Opencart 2.3, all its modification is only this code in the footer.tpl file. When you insert this code and refresh the page, a Bad request error is returned, error code: 400.
Tell me, what prevents it from working normally? I will be very grateful

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question