简体   繁体   中英

Download a csv file in php that is triggered through a url

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);

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

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.

\n

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM