0

I would like to perform multiple steps with PHP cURL.
First, I would like connect an interface protected with Login / Password and then I would like to submit a form in order to get .csv file generated by this form data.

  1. Perform cURL Auth
  2. Perform form submission with specific datas
$curl = curl_init();

$params = array(
  'export_filter[typeExport]'=>'lead',
  'export_filter[pos]' => '0',
  'export_filter[date]'=> '{"start":"2019-09-16","end":"2019-09-23"}',
  'export_filter[use_close_date]'=> '0',
  'export_filter[_token]'=> 'ANf-0IKcfi5nAgJWaGyZdl9axeU8OCxjNSrMyFHyf4'
);

$param = http_build_query($params);

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://myfakedomain.net/export/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $param,
  CURLOPT_HTTPHEADER => array(
    "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
    "accept-encoding: gzip, deflate, br",
    "accept-language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
    "authority: myfakedomaine.net",
    "cache-control: max-age=0,no-cache",
    "content-type: application/x-www-form-urlencoded",
    "cookie: device_view=full; _ga=GAxxxxx; _gid=GAxxxxx; _gat=1; PHPSESSID=8bf2766d18ddxxxxxxxxxab7927b6b",
    "origin: https://myfakedomain.net",
    "referer: https://myfakedomain.net/export/",
    "sec-fetch-mode: navigate",
    "sec-fetch-site: same-origin",
    "sec-fetch-user: ?1",
    "upgrade-insecure-requests: 1",
    "user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
  ),
));
$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
  1. Get dynamic .csv file generated by this form and download in browser (in the same form page)

Is it possible to do that ?
Where do I begin ? Thanks

Pablo DelaNoche
  • 677
  • 1
  • 9
  • 28
  • Have you read about [cookie jar](https://curl.haxx.se/docs/http-cookies.html) of cURL? It could probably help. – Koala Yeung Sep 23 '19 at 08:39
  • @KoalaYeung thanks, I will look into to perform cURL Auth – Pablo DelaNoche Sep 23 '19 at 09:16
  • Please get back here and comment if it works :-) – Koala Yeung Sep 23 '19 at 09:20
  • @KoalaYeung I succeed to perform cURL Auth by passing cookies informations in header. Now, I can submit datas to my form page but I get nothing in return. How can I Get the dynamic .csv file generated ? – Pablo DelaNoche Sep 23 '19 at 09:23
  • How would you usually get the file by using this form in browser? – Koala Yeung Sep 23 '19 at 09:27
  • I enter some informations and complete some fields (date, etc ...), and Submit the form (via POST). After 1 or 2 seconds, a .csv file is generated and download on browser. That's how I get the file – Pablo DelaNoche Sep 23 '19 at 09:33
  • By sending POST request to the form action url, the page might redirect you to the file instead of just giving it up as direct response. Try to use the [-L](https://davidwalsh.name/curl-follow-redirects) parameter to run the cURL request for your form. – Koala Yeung Sep 23 '19 at 09:42
  • @KoalaYeung I edited my question with some code and the -L flag equivalent in PHP cURL parameters. The output of my code is the screen of the form page with no output file... I'm lost – Pablo DelaNoche Sep 23 '19 at 10:25
  • 1
    Try to also get [the header of the response](https://stackoverflow.com/a/25118032/372172). See if that give you more hints. – Koala Yeung Sep 23 '19 at 10:29
  • 1
    Also, you may use [http_build_query](https://www.php.net/manual/en/function.http-build-query.php) to build your form value. – Koala Yeung Sep 23 '19 at 10:31
  • @KoalaYeung Thanks : I edited my question with http_build_query() and the response header is ````Content-Type →text/html; charset=UTF-8```` instead of csv ! I think the POST submit is not perform but How can I be sure ? Thanks – Pablo DelaNoche Sep 23 '19 at 10:57
  • 1
    @KoalaYeung, It works ! Thanks to you and your tips : 1st, replace with http_build_query(), and then, change header to csv worked ! Thanks – Pablo DelaNoche Sep 23 '19 at 11:01

0 Answers0