XF 2.2 Help with rest api

canina

Member
I have the forum in version 2.1.12.
I am trying to access using the rest api and without success.

(I want to respond to threads)

I have this code - but there is a 403 error.
PHP:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$threadId = 1; // ID of the thread you want to add a post to


// Set the API endpoint URL
$endpoint = 'http://123.45.678.70/index.php?api/posts/';
// Set the API key and API secret
$key = $apiKey;

// Set the request parameters
$params = array(
    'message' => 'This is a test comment',
    'user_id' =>  1, // Replace with the user ID of the user you want to post the comment as
    'thread_id' => $threadId);
// Set the request headers
$headers = array(
    'XF-Api-Key: ' . $key,
);
// Send the API request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);


// Process the API response
$data = json_decode($response, true);
if ($http_code != 200) {
    echo 'API request failed with HTTP status code ' . $http_code;
} elseif ($data && isset($data['status'])) {
    if ($data['status'] == 'ok') {
        echo 'Comment added successfully!';
    } else {
        echo 'Error adding comment: ' . $data['error'];
    }
} else {
    echo 'API response was not in the expected format';
}


Where can I get a sample code that works?
Thanks
 
Last edited:
Top Bottom