Jump to content

Download file from URL to path


SVKpneux

Recommended Posts

Hi,

 

I want to write php script to auto-save csv file from my supplier via API.

My code:

<?php
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://www.sample.com/folder/api/v1/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "authorization: ApiKey xxx",
    "cache-control: no-cache",
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

 

This works, but browser shows SAVE AS dialog...and I want save file directly to path (folder on my server)

How can I do this?

Thanks

Peter

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...