$result = curl_exec($curl);

$curlError = curl_error($curl);

$httpCode = (int) curl_getinfo(
    $curl,
    CURLINFO_HTTP_CODE
);

curl_close($curl);

/*
|--------------------------------------------------------------------------
| CURL Error
|--------------------------------------------------------------------------
*/

if ($result === false || $curlError !== '') {

    writeLog(
        'CURL ERROR | ' .
        $curlError .
        ' | IP: ' .
        $ip
    );

    respond(
        false,
        'Не удалось отправить заявку. Попробуйте позже.',
        500
    );

}

/*
|--------------------------------------------------------------------------
| Decode Telegram Response
|--------------------------------------------------------------------------
*/

$response = json_decode(
    $result,
    true
);

if (!is_array($response)) {

    writeLog(
        'INVALID JSON RESPONSE | ' .
        $result
    );

    respond(
        false,
        'Ошибка сервера.',
        500
    );

}

/*
|--------------------------------------------------------------------------
| Telegram API Error
|--------------------------------------------------------------------------
*/

if (

    $httpCode !== 200 ||

    empty($response['ok'])

) {

    writeLog(

        'TELEGRAM ERROR | HTTP ' .

        $httpCode .

        ' | RESPONSE: ' .

        $result

    );

    respond(

        false,

        'Ошибка отправки сообщения.',

        500

    );

}

/*
|--------------------------------------------------------------------------
| Success Log
|--------------------------------------------------------------------------
*/

writeLog(

    sprintf(

        'SUCCESS | %s | %s | %s',

        $name,

        $phone,

        $ip

    )

);

/*
|--------------------------------------------------------------------------
| Success Response
|--------------------------------------------------------------------------
*/

respond(

    true,

    'Спасибо! Заявка успешно отправлена.'

);