Solved

How do I export php curl results to a csv file

Posted on 2015-01-28
6
364 Views
Last Modified: 2015-01-29
How do I export the results to a csv file?      Script works fine.  If I echo $buffer2; I get 15000+ rows of data like this:  

 "1162","01/26/15","424.67","24.95","449.62","Overnight","Johnny", "Public","TX","513","1Z07109XXXXXXXXXXXX","01/26/15","116200","4820-XXXX-XXXX-XXXX", "11600"


Thanks for your help!

 Please note:  (Website URL is not shown deliberately....  you would not be able to see it anyway without logging in first.)  



$url2 = 'http://www.website.com/b2b/handleinvoicedownload.php';
	
	
	$fields = array(
	 
	    'delim'=>urlencode('csv'),
		'headerRow'=>urlencode('0'),
	    'startDate'=>urlencode('01/01/2015'),
		'endDate'=>urlencode('01/26/2015'),
		'columndata[1]'=>urlencode('orn'),
		'columndata[2]'=>urlencode('ord'),
		'columndata[3]'=>urlencode('sub'),
		'columndata[4]'=>urlencode('shc'),
		'columndata[5]'=>urlencode('tot'),
		'columndata[6]'=>urlencode('svc'),
		'columndata[7]'=>urlencode('svn'),
		'columndata[8]'=>urlencode('svs'),
		'columndata[9]'=>urlencode('pon'),
		'columndata[10]'=>urlencode('trc'),
		'columndata[11]'=>urlencode('shd'),
		'columndata[12]'=>urlencode('inv'),
		'columndata[13]'=>urlencode('td'),
		'columndata[14]'=>urlencode('null'),
		'columndata[15]'=>urlencode('null'),
		'save'=>urlencode('0'),
		'sumbit.x'=>urlencode('38'),
		'sumbit.y'=>urlencode('16')
		
		);
	


	
	//url-ify the data for the POST
	$fields_string = '';
	foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
	rtrim($fields_string,'&');
				
	$ch = curl_init(); // initialize curl handle
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_VERBOSE, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
	curl_setopt($ch, CURLOPT_AUTOREFERER, false);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,7);
	curl_setopt($ch, CURLOPT_REFERER, 'http://www.website.com/b2b/handleinvoicedownload.php');
	curl_setopt($ch, CURLOPT_URL,$url2); // set url to post to
	curl_setopt($ch, CURLOPT_FAILONERROR, 0);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
	
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
	curl_setopt($ch, CURLOPT_TIMEOUT, 50); // times out after 50s
	curl_setopt($ch,CURLOPT_POST,count($fields));
	curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
	
	
	echo $buffer2 = curl_exec($ch); // run the whole process

	
	if(curl_exec($ch) === false)
	{
	    echo 'Curl error: ' . curl_error($ch);
	    exit;
	}
                    
              

Open in new window

0
Comment
Question by:lawrence_dev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40576380
Redirect to a file

echo $buffer2 >anyfilename.csv

Doesn't that do what you need?
0
 

Author Comment

by:lawrence_dev
ID: 40576687
Thanks for your help but I think I am missing something...

echo $buffer2 >"anyfilename.csv";  does not work

echo $buffer2 >anyfilename.csv;  does not work

How do I export this?
0
 
LVL 18

Accepted Solution

by:
Simon earned 400 total points
ID: 40576916
I think I mis-understood the context in which you're running this.

1. open a file to write to
$foutput = fopen("yourfile.csv", "w");

2. write to the file
fwrite($foutput, $buffer2);

3. close the file
fclose($foutput);
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 17

Assisted Solution

by:Shinesh Premrajan
Shinesh Premrajan earned 100 total points
ID: 40577109
Just place these header infromation in the top of the page

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");


Hope This Helps
0
 

Author Comment

by:lawrence_dev
ID: 40577647
SimonAdept & shinuq,
THANK YOU for the simple answer!!  Very EASY to understand!!  

Why do some experts on EE have to make things so unnecessarily complicated with links to PHP.net and overly complicated answers???  SMH!!
0
 

Author Closing Comment

by:lawrence_dev
ID: 40577649
Excellent!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question