XF 2.3 api-endpoints/#type_Attachment

PHP:
<?php

$attachmentId = 1234;
$apiKey = '111111';
$apiUser = '1';

$url = "https://www.forum.com/api/attachments/$attachmentId/data";

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "XF-Api-Key: $apiKey",
    "XF-Api-User: $apiUser"
]);
curl_setopt($ch, CURLOPT_HEADER, true);

$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headersRaw = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);

curl_close($ch);

// Parse Content-Type from headers
$contentType = null;
foreach (explode("\r\n", $headersRaw) as $headerLine) {
    if (stripos($headerLine, 'Content-Type:') === 0) {
        $contentType = trim(substr($headerLine, strlen('Content-Type:')));
        break;
    }
}

// Output basic info
echo "HTTP Status: $httpCode\n";
echo "Content-Type: $contentType\n";
echo "Content-Length: " . strlen($body) . " Bytes\n";

// Check response
if ($httpCode !== 200) {
    echo "Server responded with error code $httpCode\n";
    echo "Body (truncated):\n" . substr($body, 0, 300) . "\n";
    exit;
}

if (stripos($contentType, 'html') !== false) {
    echo "Received HTML – likely an error page\n";
    echo substr($body, 0, 300) . "\n";
    exit;
}

// Optional: Check if it's a valid JPEG (first two bytes = 0xFFD8, last two = 0xFFD9)
if (substr($body, 0, 2) === "\xFF\xD8" && substr($body, -2) === "\xFF\xD9") {
    echo "JPEG image confirmed by magic bytes\n";
} else {
    echo "Binary content received, but file type unclear\n";
}
 
The attachment exists and is visible in the forum.
The API is enabled and working correctly – for example, we can successfully fetch threads.

Everything seems fine so far – except this one API call. Unfortunately, I have no idea how to access the attachments inside the posts anymore. We don't have the data_id, and this API endpoint appears to be the only way to get the file – but it doesn't work.

Code:
{
  "errors": [
    {
      "code": "server_error_occurred",
      "message": "A server error occurred while processing the API request. Please try again later.",
      "params": []
    }
  ]
}
 
Back
Top Bottom