0

There is a site https://salmonprice.nasdaqomxtrader.com/public/report;jsessionid=110C4ADED76BEEE6E08401DFF1539728?0 from where we need to download an excel file on a periodical basis. The file link is below:

https://salmonprice.nasdaqomxtrader.com/public/report?0-3.ILinkListener-loginMenu-downloadIndexHistoryLink

When we click the above link the excel file started to download.

I am trying in the following way to save the file in PHP:

$url = https://salmonprice.nasdaqomxtrader.com/public/report;jsessionid=AC40D42FFE3C6F71B7C0D74F7545AC87?0-3.ILinkListener-loginMenu-downloadIndexHistoryLink';
$file_name = "test.xls";        
if(file_put_contents( $file_name,file_get_contents($url))) {
    echo "File downloaded successfully";
}
else {
    echo "File downloading failed.";
}

but this attempt downloading web page content not excel file content. How can I solve this problem?

karim_fci
  • 1,212
  • 2
  • 17
  • 36
  • `file_get_contents` is the most basic way to download something from a remote source and you sometimes don't get the best error messages from it. In fact, when it fails it returns false but only emits a warning. Regardless, turn on full error reporting in PHP to see if you can see the warning. There are a myriad of reasons why it won't work, the most likely case is that the remote site is detecting that you are a robot. You should contact that site to ask for permission. You can also look into [alternative methods](https://stackoverflow.com/q/3979802/231316) to download files with more power. – Chris Haas Nov 04 '20 at 14:55
  • This is the best way to achieve this : [Use cURL](https://stackoverflow.com/a/3979882/6117399) – Burhan Kashour Nov 04 '20 at 15:00
  • Have you tried the link in a different browser (or incognito). Maybe they use a cookie to serve you the right file. That does not exist for new users. – T. van den Berg Nov 04 '20 at 15:02
  • Are you sure `jsessionid` does not change? – Markus Zeller Nov 04 '20 at 15:10
  • Yes, It is changing – karim_fci Nov 04 '20 at 15:19
  • Maybe that could be the problem. Can you make sure tracking it, so that `$url` always has the correct one? When the `jsessionid` is invalid you will for sure not get the expected content. – Markus Zeller Nov 04 '20 at 19:11

0 Answers0