'Downloading .xlsx file from URL using PHP

I'm having issues downloading an excel file onto my web server from a URL using PHP. The file is publicly accessible on a Microsoft Sharepoint server and downloads automatically when accessed directly via the link. Here is my code:

$url = 'https://example.sharepoint.com/somefile.xlsx?download=1';
$file = file_put_contents('template.xlsx', file_get_contents($url));

The script successfully creates the template.xlsx file on the server, however the file itself is 0 bytes and when I try to open it I get an error along the lines of "Excel cannot open the file because the file format or file extension is invalid". What am I missing?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source