Bart Klaster
Bart Klaster

Reputation: 53

Get CSV with cURL or File get conent and sent post values with it

I have a problem with cURL(I also tried it with file get content?). My starting point is a webserver that supplies me with an CSV file. To get the CSV file I've got to post some data to it (login details)

I have the following script already:

$url ='https://services.easyposonline.nl/i-shop/services/exporter/csv/artikelen.csv';
$httpRequest = curl_init();

curl_setopt($httpRequest, CURLOPT_RETURNTRANSFER, true);
curl_setopt($httpRequest, CURLOPT_HTTPHEADER, array("Content-Type: text/csv"));
curl_setopt($httpRequest, CURLOPT_POST, true);
curl_setopt($httpRequest, CURLOPT_HEADER, true);

curl_setopt($httpRequest, CURLOPT_URL, $url);
curl_setopt($httpRequest, CURLOPT_POSTFIELDS, $data);

$returnHeader = curl_exec($httpRequest);
print_r($returnHeader);
curl_close($httpRequest);

$data has my login detail for the post.

When posting this I get this back:

HTTP/1.1 100 Continue HTTP/1.1 200 OK Date: Wed, 11 Dec 2013 09:15:30 GMT Server: Apache X-Powered-By: PHP/5.4.21 Set-Cookie: PHPSESSID=rqse865bstv22vn9rdaefqvfg3; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache x-epsw-login: ok x-epsw-server: iepsw,version=V1.12.3.120911,user=ep_5757,app=i-shop,appid= Transfer-Encoding: chunked Content-Type: text/plain ||||||||||||||||| 4000270017|00800|4-20* Toilettasje|4-20* Toilettasje|0|Berba|4-020||00|1|4000|0|||v000270.jpg|4|19990929| 4000270025|00800|4-20* Toilettasje|4-20* Toilettasje|0|Berba|4-020||01|1|4000|0|||v000270.jpg|0|19990929| 4000270033|00800|4-20* Toilettasje|4-20* Toilettasje|0|Berba|4-020||02|1|4000|0|||v000270.jpg|0|19990929| 4000270041|00800|4-20* Toilettasje|4-20* Toilettasje|0|Berba|4-020||30|1|4000|0|||v000270.jpg|0|19990929| 4000270059|00800|4-20* Toilettasje|4-20* Toilettasje|0|Berba|4-020||06|1|4000|0|||v000270.jpg|0|19990929| 4000270067|00800|4-20* Toilettasje|4-20* Toilettasje|0|Berba|4-020||07|1|4000|0|||v000270.jpg|2|19990929| 4000270075|00800|4-20* Toilettasje|4-20* Toilettasje|0|Berba|4-020||15|1|4000|0|||v000270.jpg|0|19990929| 4000270083|00800|4-20* Toilettasje|4-20* Toilettasje|0|Berba|4-020||16|1|4000|0|||v000270.jpg|0|19990929| 4000270091|00800|4-20* Toilettasje|4-20* Toilettasje|0|Berba|4-020||17|1|4000|0|||v000270.jpg|0|19990929| 

How to retrieve the webpage as a valid CSV mimetype file?

Upvotes: 0

Views: 5989

Answers (1)

chanchal118
chanchal118

Reputation: 3647

<?php


$cookie_file_path = "downloads/cookie.txt";
$fp = fopen($cookie_file_path, "w");
fclose($fp);

$loginUrl = '/url/to/log in to site';
$downloadUrl = '/path/to/ csv file';

$loginPostInfo = array(
    // data that need to be posted
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);


curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $loginPostInfo);
curl_exec($ch);

// now curl request for download file
$filePath = "downloads/foo.csv";
$file = fopen($filePath, "w+");

curl_setopt($ch, CURLOPT_URL, $downloadUrl);
curl_setopt($ch, CURLOPT_FILE, $file);
$result = curl_exec($ch);

fclose($file);
curl_close($ch);

Upvotes: 3

Related Questions