<?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";
}