Downloading an XML File using Magento and cURL
In this blog post, we will go through the process of downloading an sitemap_index.xml
file from a remote server using Magento's html-magmi-web-download_file.php
script. This is particularly useful when dealing with large sites or when the XML files are not publicly accessible through a simple URL request.
Prerequisites
Make sure your Magento installation has the
Magmi
extension installed and properly configured. You can find more information about Magmi on their official GitHub page: MagmiReplace
<YOUR_REMOTE_URL>
in the examples below with the actual URL of your remotesitemap_index.xml
file.Set up your Magento site to allow cURL requests from the script by updating the
local.xml
orapp/etc/env.php
file (depending on your Magento version) with the following lines:<config> <global> <web> <unsecured-methods> <allow/> </unsecured-methods> </web> </global> </config> ``` or ```php const ENV_MAGENTO_ADMIN_DOWNLOAD_DISABLE = false;
The Script
Now, let's write a simple PHP script to download the sitemap_index.xml
file using Magento and cURL. Save the following code as downloadXMLfile.php
in your Magento root directory:
<?php
require 'app/Mage.php';
umask(0); // optional, set permissible for newly created files
Mage::app()->setUseSessionInUrl(false);
$url = '<YOUR_REMOTE_URL>';
$xmlFileName = 'sitemap_index.xml';
try {
$fileHandler = Mage::getModel('core/file_storage', array('filename' => $xmlFileName));
$magmi = new Magmi_ImportExport();
$response = $magmi->importFile('webdownload', array(
'source' => $url,
'destination' => Mage::getBaseDir('media') . DS . $fileHandler->getPath(),
));
if ($response['error'] != '') {
throw new Exception($response['message']);
} else {
echo "File downloaded successfully: {$xmlFileName}";
}
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
Replace <YOUR_REMOTE_URL>
with the actual URL of your remote file. Make sure the script has write permissions to create and save files in your Magento media directory.
Executing the Script
To run the script, open a terminal or command prompt window, navigate to your Magento root directory, and execute:
php downloadXMLfile.php
This script will download the sitemap_index.xml
file from the remote server using cURL and save it to the media
folder of your Magento installation. Once done, you can process the XML data within Magento as needed.
Keep in mind that this example assumes a local Magento installation. If you're working on a live site, consider using proper access control and security measures for downloading files from the internet.