What are the best practices for sending messages through ICQ using PHP?

Sending messages through ICQ using PHP can be achieved by utilizing the ICQ API. The best practice is to first obtain an access token from ICQ OAuth API and then use that token to send messages to the desired ICQ user. It is important to handle errors and validate the response from the API to ensure the message is successfully delivered.

<?php

// ICQ API endpoint
$api_url = 'https://api.icq.net/bot/v1/messages/sendText';

// ICQ bot token
$token = 'YOUR_ICQ_BOT_TOKEN';

// ICQ user ID
$user_id = 'ICQ_USER_ID';

// Message to send
$message = 'Hello from ICQ!';

// Data to send
$data = [
    'chatId' => $user_id,
    'text' => $message
];

// Initialize cURL session
$ch = curl_init($api_url);

// Set cURL options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Authorization: Bearer ' . $token
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute cURL session
$response = curl_exec($ch);

// Close cURL session
curl_close($ch);

// Handle response
if ($response === false) {
    echo 'Error sending message';
} else {
    $response_data = json_decode($response, true);
    if ($response_data['ok']) {
        echo 'Message sent successfully';
    } else {
        echo 'Error: ' . $response_data['description'];
    }
}

?>