views:

256

answers:

2

Hi there,

I'm trying to use a post form to generate an excel file after choosing which data to grab from a MySQL database. I'm using cURL because I essentially want to double-post: first to save any settings of which fields are being used, and second to generate the Excel file, all with one button.

I have a PHP page set up with the correct headers to generate a simple Excel file from tab-separated content (not dealing w/ line endings or tabs in the fields, just keeping it simple). If I hit that page directly with the browser (and include dummy data in the PHP), my browser downloads an Excel file.

I'm getting the data through to this page via cURL post (if I don't send headers via cURL--??).

And here's about where I get lost:

How do I create/download the data as an Excel file via cURL?

My guess is it's something to do w/ the headers?

Setting headers in PHP worked fairly well:

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Pragma: public");
header("Cache-control: max-age=0");

When I set them via cURL instead, I did this (well, these are all of my cURL options):

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $foo);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Disposition: attachment; filename=test.xls',
    'Content-Type: application/vnd.ms-excel; charset=UTF-8',
    'Pragma: public',
    'Cache-control: max-age=0'
));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response_code = curl_exec($ch);
if ($response_code!== false) {
    echo 'response: '.$response_code;
} else {
    echo 'error: '.curl_error($ch);
}
curl_close ($ch);           

When I do this, the cURL $response_code I get on the sending page looks like this (I've broken the lines up - it's all one line):

HTTP/1.1 200 OK 
Date: Sun, 31 Jan 2010 20:43:38 GMT 
Server: Apache 
Transfer-Encoding: chunked 
Content-Type: text/html

When I don't include the headers via cURL, $_POST is working. But when I do include the headers, $_POST does not work.

That Transfer-Encoding: chunked seems like a clue. But initial research didn't help me, partly because, as I mentioned, I'm lost.

Any ideas?

Thank you!
-Jeremy

A: 

Well i beleive CURLOPT_HTTPHEADER sets the headers for the request... not the response. As far as $_POST goes what do you mean it doesnt work? Did you set the CURLOPT for a post request (the default is get)?

prodigitalson
Thanks for your quick response! - I was editing my question to include my entire `CURLOPT` settings.When I include `CURLOPT_HTTPHEADER` (and `CURLOPT_HEADER`), the $_POST data doesn't show up in the echoed `$response_code`. But when I skip the cURL headers, the $_POST data does show up in the response.
if you set the headers manually you ned to set the post header manually as well i think. Also as i said before you cant control the response headers those are determined by the server supplying the response. If youre trying to upload the file - im pretty sure the headers for the file dont matter because its not going to write that as file meta-data - that mime type will only be used by the recieving client - in this case the server. They wont have any bearing on what happens when that file is then accessed on the server you uploaded it to.
prodigitalson
A: 

I think you are fundamentally mistaken about the way content is delivered. The content-type headers just point out what format to expect, but you still have to actually generate data in that format.

Looking at the Excel file format, that is not a trivial task. There is a PHP based class that uses COM to write an Excel file (and works only on a Windows server): here I haven't used it myself though.

Then there's PHPExcel that makes use of the new, XML-Based Excel format and works platform independently. I don't know this either, but it looks good.

Remember, there's always the possibility of delivering plain old CSV, and opening that in Excel. It leaves with you with a lot less options (no column design etc.) but would work out-of-the-box without any libraries or extensions.

Also, I'm unsure what you want to use cURL for in this scenario? You are not trying to download anything on the server side, if I understand it correctly, are you?

Pekka
Thanks - yes, I'm really just generating a CSV (actually, it's tab separated). By adding the headers, the browser generates the file automatically. This works fine when I access the page directly.
Ah. But then I don't understand what the problem is? What are you using CURL for?
Pekka
Are you looking for how to include a file in a CURL request? Then maybe this helps you: http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html
Pekka
Yes exactly why are you using curl? Is this file on a remote server or your server?
prodigitalson
Also... yes, trying to download an excel file of data. I'm not sure I really need to use cURL. But it seems like I need to post twice...
so iuf you are trying to download the excel file form another server then jsut fire off the request... the response headers arent going to matter. then you need to either echo that file to the client or save it to the server... if you are sending directly to the client then set the headers in php as you did in your first code box then echo the curl response text. If youre downloading it to the local server then jsut use `file_put_contents`
prodigitalson
"Why I am using cURL" is a good question. Might be barking up the wrong tree.If I just include the PHP to make the Excel file on the same page as the post form, then it works like a snap.BUT I want to be able to save settings on this form page as well, and I want those to show up on the same page.E.g.: Export template #1 shows name, address, phone, interests. Export template #2 shows name, email, emergency contact info.If I could just redirect after generating the Excel page, I'd be set. But it doesn't seem like I can.
No you cant redirect because youve already sent the headers at that point. What you could do is just write the file to disk then redirect and wherever you redirect to could serve the excel file as normal.
prodigitalson
Actually, I thought of that, but I want to redirect back to the same page I'm on, with, say, a 'success' message. I turned to cURL because I wanted to simultaneously re-load the current page AND download the generated file. I also don't really want to leave files on the server. I think I'm letting this go, and I'm just going to check if I'm saving the settings for a new data export template. If so, then I will reload the page w/ the template selected, and the user will have to just post again to actually download. Meh.
I'm at work and not really fully following, but have you considered posting your form with a `target=_blank`? That way, you could redirect to a success page on your current page, and the new window would turn into the CSV file download.
Pekka