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);
w+brather thanw+, as a general rule of thumb. (Windows-OSs does terrible things in the defaulttmode, and the same used to be true for MacOS <=9, i believe. but for portability's sake, usew+b)