API not working EspoCRM

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coca22052022
    Junior Member
    • Apr 2023
    • 19

    API not working EspoCRM

    Hello, I want to create leads through the EspoCrm API.
    I created an API user, gave it maximum capabilities, issued an API key.

    I created this code. But the lead is not created.

    I want to create a lead through a custom API.

    HELPP MEEE

    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
        <title>Создание лида</title>
    </head>
    <body>
        <h2>Создание лида</h2>
        <form method="POST" action="create_lead.php">
            <label for="name">Имя:</label>
            <input type="text" id="name" name="name" required><br>
    
            <input type="submit" value="Создать лид">
        </form>
    </body>
    </html>
    HTML Code:
    <?php
    
    $apiUrl = 'http://194.67.67.220/api/v1/';
    $apiKey = '98e8d19bf8d51286ce1b613ec4b7db05';
    
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        // Получение данных из формы
        $name = $_POST['name'];
    
        // Данные для создания лида
        $data = [
            'data' => [
                'type' => 'Lead',
                'attributes' => [
                    'name' => $name
                ]
            ]
        ];
    
        // Преобразование данных в формат JSON
        $dataJson = json_encode($data);
    
        // Инициализация cURL-соединения
        $ch = curl_init($apiUrl . 'Lead');
    
        // Установка необходимых заголовков
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json',
            'Authorization: Bearer ' . $apiKey
        ]);
    
        // Выполнение запроса
        $response = curl_exec($ch);
    
        // Проверка наличия ошибок
        if ($response === false) {
            die('Ошибка: ' . curl_error($ch));
        }
    
        // Получение информации о статусе запроса
        $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
        // Закрытие cURL-соединения
        curl_close($ch);
    
        // Обработка ответа
        if ($responseCode === 201) {
            // Лид успешно создан
            echo 'Лид успешно создан.';
        } else {
            // Возникла ошибка при создании лида
            echo 'Ошибка при создании лида: ' . $response;
        }
    }
    ?>
    ​
    ​



    Please provide a working form script.

    And tell me what could be the reason why the lead is not being created​
    Last edited by coca22052022; 06-22-2023, 02:46 PM.
  • esforim
    Active Community Member
    • Jan 2020
    • 2204

    #2
    Hi there, my first question is: Have you tested your API? Can you get that to work?

    As for your code, unfortunately that is out of my skill level, I leave it to someone how to help you. Also with the magic of AI, you might want to just give that a try also, see if they have spot an error or recommendation for you.

    Comment

    Working...