Class 'SimpleXMLElement' not found: When Local Works but Production Fails
You are encountering a classic environment discrepancy. Your local development machine has SimpleXML loaded, and your DigitalOcean droplet reports that SimpleXML is present in the list of loaded extensions. Yet, when your Laravel application attempts to interact with DigitalOcean Spaces, it throws Class 'SimpleXMLElement' not found.
The stack trace points specifically to RestXmlParser.php inside a PayloadParserTrait. This indicates the error isn't happening during your file upload logic (Storage::put), but rather within the AWS SDK component trying to parse the XML response from a DigitalOcean API call. The SDK is attempting to instantiate SimpleXMLElement to deserialize an XML payload, and that class definition is missing at runtime despite the extension being listed as loaded.
The Extension Registration Paradox
The core issue lies in how PHP extensions are registered versus how they are loaded. Your get_loaded_extensions() output confirms that SimpleXML is present. However, in many containerized environments or specific Linux distributions (like Alpine Linux often used for lightweight droplets), there can be a mismatch between the system's libxml libraries and the PHP extension binaries.
Sometimes, an extension appears in the list because the shared library (libxml) is detected, but the PHP-specific implementation of SimpleXML classes isn't fully initialized or is being shadowed by a conflict. More critically, if you are using a specific version of the AWS SDK for PHP (v3), it relies heavily on native XML parsing. If the underlying C extension for SimpleXML is compiled without proper flags or linked against an incompatible libxml2, the class might be declared but not instantiated correctly when accessed by third-party libraries like the AWS SDK.
Investigating the Environment
Before assuming a missing package, verify the actual PHP binary being used by your Laravel worker versus the one showing the loaded extensions. If you are using Supervisor or Swoole to run your queue workers, ensure they are pointing to the same PHP version and binary as the CLI tool you used to check get_loaded_extensions().
Run the following command inside your project directory to verify consistency:
php -m | grep simplexml
If this returns nothing, but get_loaded_extensions() in Tinker shows it, there is a fundamental environment mismatch. The most reliable fix is to ensure the PHP process running your application has the xml and simplexml modules explicitly enabled in its php.ini.
Resolving the DigitalOcean Spaces Integration
The code snippet you provided uses Storage::put with a file path constructed from a username and timestamp. This logic relies on the local filesystem or an S3-compatible driver. Since you are using DigitalOcean Spaces, you should ensure your config/filesystems.php is configured to use the s3 driver pointing to your bucket.
If you stick with the current implementation, the error occurs during the API handshake to verify the bucket location (GetBucketLocationParser). The AWS SDK tries to parse the XML response from DigitalOcean. If that parsing fails because SimpleXMLElement is effectively unavailable to the SDK's internal logic, the upload chain breaks before your Storage::put method is even reached.
To fix this permanently on your Droplet:
- Check the PHP Version: Ensure you are running a supported PHP version (7.4 or higher recommended for Laravel). Older versions sometimes had stricter requirements for XML extensions.
- Reinstall PHP Extensions: If you installed PHP via
aptoryum, ensure you have reinstalled the specific packages required. For Debian/Ubuntu, this is often:
Note that on some minimal images,sudo apt-get update sudo apt-get install php-xml php-simplexmlphp-xmlmight be a meta-package that doesn't pull in the full implementation unless explicitly requested. - Verify Runtime: Restart your PHP-FPM or Swoole service after reinstalling:
sudo systemctl restart php-fpm81 # Replace with your specific version
Final Verification
Once the environment is corrected, clear your Laravel cache to ensure no stale configuration persists:
php artisan config:clear
php artisan route:clear
Try running your upload command again via Tinker or a direct request. The AWS SDK should now successfully parse the XML response from DigitalOcean Spaces, allowing the GetBucketLocationParser to complete its job without throwing a FatalThrowableError. If the issue persists on a specific Droplet image, consider switching to the official DigitalOcean Docker images or switching your PHP version, as newer distributions have improved compatibility with strict XML parsing requirements.