notatardis
Member
New to the API requests, but I'm trying to set up a test run for creating a new conversation using cUrl in php and a super user api key.
PHP:
<?php
// API endpoint URL
$url = 'https://***.com/site/api/conversations';
// API key
$api_key = '****';
// Recipient IDs
$recipient_ids = [663];
// Conversation title
$title = 'API Test';
// Conversation message body
$message = 'Testing the API';
// Data to be sent in the request
$data = [
'recipient_ids' => json_encode($recipient_ids),
'title' => $title,
'message' => $message,
'attachment_key' => '', // No attachment key in this case
'conversation_open' => true,
'open_invite' => false // Change to true if needed
];
// Request headers
$headers = [
'Content-Type: application/x-www-form-urlencoded',
'XF-Api-Key: ' . $api_key,
'XF-Api-User: 663'
];
// Initialize cURL session
$curl = curl_init();
// Set cURL options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Execute cURL request
$response = curl_exec($curl);
// Check for errors
if ($response === false) {
echo 'cURL error: ' . curl_error($curl);
} else {
// Decode JSON response
$data = json_decode($response, true);
// Check if response contains errors
if (isset($data['errors'])) {
foreach ($data['errors'] as $error) {
echo 'Error code: ' . $error['code'] . ', Message: ' . $error['message'] . "\n";
}
} else {
// Output response
echo 'Response from API: ' . $response;
}
}
// Close cURL session
curl_close($curl);
?>