<?php
$url = "https://dp.apis.net.tr/v3beta";
$apiToken = "098bc358074b3aa569f60f66b591c55a";

$payload = [
    "senderId" => 111,
    "message" => "Selam bu bir testtir",
    "receiverId" => 222,
    "history_messages" => [
        "kullanici: merhaba",
        "bot: merhaba",
        "kullanici: nasılsın"
    ],
    "details" => [
        "user_isim" => "Cankat",
        "user_sehir" => "Kayseri",
        "user_yas" => 30,
        "user_cinsiyet" => "erkek",
        "bot_isim" => "Melis",
        "bot_yas" => 25,
        "bot_sehir" => "Kayseri",
        "bot_cinsiyet" => "kadin"
    ]
];

$json = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

$ch = curl_init($url);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $json,
    CURLOPT_HTTPHEADER => [
        "Content-Type: application/json",
        "Authorization: Bearer $apiToken"
    ],
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, // IPv6 takılmasın
]);

$response = curl_exec($ch);
$err = curl_error($ch);
$info = curl_getinfo($ch);
curl_close($ch);

header('Content-Type: text/plain; charset=utf-8');

echo "HTTP KODU: " . $info['http_code'] . "\n\n";

if ($err) {
    echo "cURL HATASI: $err\n";
} else {
    echo "YANIT:\n" . ($response ?: "<boş yanıt>") . "\n";
}
