How to fetch a file with PHP
If you want to fetch a file from another server, PHP can be used.
Example, I want to download the Clickbank Product XML file, also known as the Marketplace feed, from Clickbank and store it on my server.
The Clickbank Marketplace XML feed address is https://accounts.clickbank.com/feeds/marketplace_feed_v2.xml.zip
I can create a PHP file that will fetch it like this:
<?php
$fileToFetch = 'https://accounts.clickbank.com/feeds/marketplace_feed_v2.xml.zip';
$whereToSave = fopen(dirname(__FILE__) . '/marketplace_feed_v2.xml.zip', 'w+');
$curl = curl_init($fileToFetch);
curl_setopt_array($curl, [
CURLOPT_URL => $fileToFetch,
CURLOPT_BINARYTRANSFER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FILE => $whereToSave,
CURLOPT_TIMEOUT => 50,
]);
$response = curl_exec($curl);
if ($response === false) {
throw new \Exception('Curl error: ' . curl_error($curl));
}
$response;
?>
Just so you know, you'll need the PHP curl extension installed. On Debian or Ubuntu, you can install it by:
sudo apt-get install php-curl
You'll know you don't have php-curl installed if you get an error like the following:
PHP Fatal error: Uncaught Error: Call to undefined function curl_init()