WordPress REST API: Große Bild-Uploads erreichen das Backend trotz korrekter PHP-Einstellungen nichtPhp

PHP-Programmierer chatten hier
Anonymous
 WordPress REST API: Große Bild-Uploads erreichen das Backend trotz korrekter PHP-Einstellungen nicht

Post by Anonymous »

Problembeschreibung:
Ich entwickle eine Chat-Anwendung mit Bild-Upload-Funktion mithilfe der WordPress-REST-API. Wenn Benutzer mehrere große Bilder (3 Bilder) anhängen, erreicht die Anfrage den Backend-API-Endpunkt nicht, aber kleinere Bilder funktionieren einwandfrei.
Hauptsymptome:
  • Kleine 3 Bilder funktionieren ohne Fehler
  • Große Bilder lösen keine Backend-Protokolle oder Fehler aus
  • Keine WordPress-Debug-Protokolle generiert
  • Serveranbieter bestätigt, dass keine serverseitigen Probleme gefunden wurden
Umgebung
  • WordPress: 6.0+
  • PHP: 8.2
  • Server: Apache (Shared Hosting)
PHP-Einstellungen:
  • upload_max_filesize = 600M
  • post_max_size = 650M
  • max_execution_time = 500
  • max_input_time = 500
  • memory_limit = 300M
  • max_input_vars = 3000
Frontend-JavaScript-Code

Code: Select all

function sendImageRequest(question, imagesArray, country, conversationHistory) {
console.log('🖼️ Starting image analysis request with', imagesArray.length, 'images...');

// Prepare image data array with pure base64
const imageDataArray = imagesArray.map(img => {
let pureBase64 = img.data;
if (img.data.startsWith('data:')) {
pureBase64 = img.data.split(',')[1];
}
return {
id: img.id,
data: pureBase64,  // Base64 string
name: img.name
};
});

const requestBody = {
question: question,
image_data: imageDataArray, // Array of base64 images
country: country,
conversation_history: conversationHistory
};

console.log('📤 Image request payload with', imageDataArray.length, 'images, total size:', JSON.stringify(requestBody).length, 'bytes');

$.ajax({
url: '/wp-json/chat2find/v1/image',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(requestBody),
success: function(response) {
console.log('✅ Image analysis successful');
},
error: function(xhr, status, error) {
console.error('❌ Image analysis error:', error);
}
});
}
Backend-WordPress-PHP-Code

Code: Select all

class API_Handler {
public function handle_image_request(WP_REST_Request $request) {
try {
error_log('🎯 Image endpoint reached'); // This never fires for large images

$params = $request->get_params();
$question = sanitize_text_field($params['question'] ?? '');
$image_data = $params['image_data'] ?? [];
$country = sanitize_text_field($params['country'] ?? '');
$conversation_history = $params['conversation_history'] ?? [];

if (empty($question)) {
return new WP_Error('invalid_input', 'Question is required', array('status' => 400));
}

if (empty($image_data) || !is_array($image_data)) {
return new WP_Error('invalid_input', 'Image data array is required', array('status' => 400));
}

// Validate each image data
foreach ($image_data as $img) {
$img_data = is_array($img) ? ($img['data'] ?? '') : $img;
if (!preg_match('/^[a-zA-Z0-9+\/]+={0,2}$/', $img_data)) {
return new WP_Error('invalid_input', 'Invalid image data format', array('status' => 400));
}
}

// Limit to maximum 3 images
if (count($image_data) > 3) {
return new WP_Error('invalid_input', 'Maximum 3 images allowed', array('status' => 400));
}

// External API call
$api_endpoint = get_option('chat2find_api_endpoint', 'https://example.com/api/');
$image_url = rtrim($api_endpoint, '/') . '/image';

$response = $this->make_api_call($image_url, [
'question' => $question,
'image_data' => $image_data,
'country' => $country,
'conversation_history' => $conversation_history
], false);

if (is_wp_error($response)) {
error_log('Chat2Find Image API Error: ' .  $response->get_error_message());
return new WP_Error('api_error', 'Image analysis service unavailable', array('status' => 503));
}

$response_data = json_decode($response, true);
return new WP_REST_Response($response_data, 200);

} catch (Exception $e) {
error_log('Chat2Find Image Exception: ' . $e->getMessage());
return new WP_Error('internal_error', 'Internal server error', array('status' => 500));
}
}

public function register_routes() {
register_rest_route('chat2find/v1', '/image', array(
'methods' => 'POST',
'callback' => array($this, 'handle_image_request'),
'permission_callback' => array($this, 'check_permission')
));
}

public function check_permission() {
return true;
}
Debugging-Versuche
WordPress-Debug-Protokollierung

Code: Select all

// In wp-config.php
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
Beobachtungen
Kleine Bilder (je ca. 100 kb): funktioniert
Große Bilder (je ca. 3 MB): Anforderungsgröße ca. 12–15 MB, erreicht nie das Backend
Registerkarte „Netzwerk“: Zeigt an, dass die Anforderung gesendet, aber keine Antwort empfangen wurde.
Keine Serverfehler in Apache/WordPress-Protokolle
Keine PHP-Fehler, selbst bei aktiviertem Debugging
Frage
Was könnte große JSON-Anfragen stillschweigend blockieren, bevor sie WordPress erreichen? Wie kann ich das beheben?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post