0

I try to download a csv file that is triggered through a URL. I do the get request and then i get the result back but the contents are just HTML code. I don't get back the csv file. If i print_r the results of the get request then the csv file starts to download in the browser but i need to download the csv file within PHP.

Is there any way to download the csv file from within PHP? Any help will be appreciated. Below is my code so far.

the get request...

require_once('hhb_.inc.php');
    $hc = new hhb_curl('', true);

            $output_filename = "hotProduct_Laptop_Parts.csv";
                $fp = fopen($output_filename, 'w+');
                set_time_limit(0); // unlimited max execution time
                $contents=$hc->setopt_array(array(
                  CURLOPT_FILE    => $fp,
                  CURLOPT_RETURNTRANSFER => 1,
                  CURLOPT_FOLLOWLOCATION=> true,
                  CURLOPT_TIMEOUT =>  28800, // set this to 8 hours so we dont timeout on big files
                  CURLOPT_URL     => 'https://portals.aliexpress.com/adcenter/hot_product_download.do?categoryId=200001083&categoryName=Laptop%20Parts%20&%20Accessories',
                  CURLOPT_HTTPHEADER => array(
                      'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                      'Referer: https://portals.aliexpress.com/adcenter/hotProductRecommend.htm?categoryId=7',
                      'accept-language: en-US,en;q=0.5',
                      'accept-encoding: gzip, deflate, br',
                      'upgrade-insecure-requests:   1',
                      'te:  trailers',
                      'authority:   portals.aliexpress.com'
                       ,

                  )
                )
                )->exec()->getStdOut();

        print_r($contents);


            // the following lines write the contents to a file in the same directory (provided permissions etc)

            fwrite($fp, $contents);
            fclose($fp);
3
  • 1
    Are you sure the returned page is not running a javascript that triggers the actual download? Commented Aug 4, 2019 at 10:11
  • 1
    you should probably use fopen mode w+b rather than w+, as a general rule of thumb. (Windows-OSs does terrible things in the default t mode, and the same used to be true for MacOS <=9, i believe. but for portability's sake, use w+b) Commented Aug 4, 2019 at 19:43
  • @FedericoklezCulloca i get this html code back pastebin.com/XwSj1qAB . it include javascript Commented Aug 4, 2019 at 20:22

2 Answers 2

1

Have you set your headers properly?

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

You need to specify them on top of your php file

Sign up to request clarification or add additional context in comments.

Comments

1

you need to be logged in, and have a valid logged-in-cookie, before you can download that file. if you try to view that URL in a browser that is not logged in, the first thing you will see is the login page! the page is basically saying "log in first".

you have not posted the code required to reproduce the issue, because you have not posted the URL required to reproduce the issue. i have voted to close the issue because i cannot reproduce it.

Your problem is almost certainly that you need to obtain a cookie prior to downloading the file (which is rather common), but because you did not share the url you are trying to download from, i cannot check that theory. we cannot help until you share the url in question.

Ok i have shared the url!
I get logged in correctly and i get back the right response. I tried loading another page and showed up. But when i try to load this specific page i get this html code back pastebin.com/XwSj1qAB ... I dont understand why!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.