Не получается интегрировать сайт на wp c amo crm через Api

Рейтинг: 0Ответов: 0Опубликовано: 18.07.2023

Не получается интегрировать сайт на wp c amo crm через Api. Выходит ошибка:

  Error 400. Wrong structure of the array of transmitted data, or invalid identifiers of custom fields. 

Код писал не сам, а руководствовался этой статьей:https://habr.com/ru/articles/650019/. Буду рад любой помощи!!! //Страница откуда отправляю

  <?php
    /*
    Template Name: Тестовый шаблон
    
    */
    
    ?>
    <?php
    require_once 'access.php';
    
    $name = 'Имя клиента';
    $phone = '+380123456789';
    $email = 'email@gmail.com';
    $target = 'Цель';
    $company = 'Название компании';
    
    $custom_field_id = 454021;
    $custom_field_value = 'тест';
    
    //$ip = '1.2.3.4';
    $domain = 'site.ua';
    $price = 0;
    $pipeline_id = 7031702;
    $user_amo = 31177614;
    
    $utm_source   = '';
    $utm_content  = '';
    $utm_medium   = '';
    $utm_campaign = '';
    $utm_term     = '';
    $utm_referrer = '';
    
    $data = [
        [
            "name" => $phone,
            "price" => $price,
            "responsible_user_id" => (int) $user_amo,
            "pipeline_id" => (int) $pipeline_id,
         
          
        ]
    ];
    
    $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, 'cookie.txt');
    curl_setopt($curl, CURLOPT_COOKIEJAR, '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') );
    

//config.php

       $Response = json_decode($out, true);
        $Response = $Response['_embedded']['items'];
        $output = 'ID добавленных элементов списков:' . PHP_EOL;
        foreach ($Response as $v)
            if (is_array($v))
                $output .= $v['id'] . PHP_EOL;
        return $output;
<?php
$subdomain     = 'pavelps23'; // поддомен AmoCRM
$client_secret = 'rahgDFygM20C2zBqmNajKHECAfCjFITMWq7j4ykoiUo0dm2NqPQ3VDelr4H6lfKI'; // Секретный ключ
$client_id     = 'c785da9b-3e19-4e4f-b097-049c744a88f6'; // ID интеграции
$code          = 'def50200fb9e6c0b78b06d22611e91dcb56873c60c8b4f37ee592aa2ade62337cd8a5c4c6133efe461a06de21450cdb3457c2a4123aa09e799a3ec3729dee277897219e9676952e75f1c603ecc05c13c050a8c20f097e73b4738467fcd0368e47fb3b0a24fa6a6446ceed5ec68853f5697cb8ebbea5a2d6c4a36fcc76a782c93899b5d1f0b199f3b43450258275a9eb656cbc588cb933eb533af86eb578f20a5307150e2b30b9096caf7d4cfd5da847b4943b03dee875995c89e0120216a3d3bca157041edabee965bb7a5c1243b31f162b6a27949f917fb761dd9b3ccda212b5c4139a8f2867bac13dee1b6f186cdf1965d2cce6312cf7fdd7d1e9057fe07fb0a33cc91ff97f9ec69e5f2063639afc560ae4494b1fe82594bd42ab79a713a7254689d49496d5ee3a45673ebed16debd85fafc8e2fc8df2b0845db3132b35d8d87e9c30d906dfa83a2038cf40b1f3f60ae60de81f43ea3765c714b88cee7e8d79063249629af100bb7ebed6b37e6ef066e75466b302fce0a48d44b24a6ea63d753ce282fd3170a0df61ee6690bc1071f8a9e0029cc81b5e915532c25b3eb0b4d0b301abb7b6b9d26b599f25ae89b74a3e50a8e7f45aea33d51aa227dcb84db25c4e38035a685a00375a19cbbf5f323dbefc1686e1ceed2a152b3d5012232d3921561618d2eeaab53665a9d33'; // Код авторизации
$token_file    = 'tokens.txt';
$redirect_uri  = 'https://specnazday.ru/amo/';
?>

//Auth.php

<?php
/*
Template Name: auth

*/

?>

<?php
require_once 'config.php';

$link = "https://pavelps23.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);

Ответы

Ответов пока нет.