Page 1 of 1

possible to send multipart form data with cURL and add files

Posted: Thu Sep 15, 2005 1:55 pm
by Burrito
arrr....can this be done?

<pirate_speak_off>
I have just been given a new challenge where I will need to post data to a different web site on regular intervals and that data will also need to include some attached files. The files will reside on my web server and, for reasons I wont' explain here, I can't ftp them.

here is kind of an outline of what I'm after:

The site to which I will be sending this data is going to create a custom "action" page to which we can submit post data over an SSL connection. On the site (the form page) they currently have, you can browse for and upload files. The action page they will create for us will have a few minor modifications to their existing one, but being able to accept files from the post data is a must.

I am goign to use cURL to POST the data and need to be able to attach files....

how's that for redundancy 8O
</pirate_speak_off>

Posted: Thu Sep 15, 2005 3:59 pm
by Ambush Commander
Failing everything else, you can always use sockets... make sure you study up on the RFC...

From the manual...

http://us2.php.net/manual/en/function.curl-setopt.php

Code: Select all

<?php

$file = "file_to_upload.txt";
$submit_url = "http://www.url_to_upload_to.com/upload_page.php";

$formvars = array("cc"=>"us \n");
$formvars[variable_1] = "bla bla \n";
$formvars[variable_2] = "bla bla \n";
$formvars[variable_3] = "bla bla \n";
$formvars[variable_4] = "bla bla \n";
$formvars[upfile] = "@$file"; // "@" causes cURL to send as file and not string (I believe)

   // init curl handle
   $ch = curl_init($submit_url);
   curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");  //initiates cookie file if needed
   curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");  // Uses cookies from previous session if exist
   curl_setopt($ch, CURLOPT_REFERER, "http://www.last_url_for_referer_logs.com");  //if server needs to think this post came from elsewhere
   curl_setopt($ch, CURLOPT_VERBOSE, 1);
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); // follow redirects recursively
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $formvars);

   // perform post
   echo $pnp_result_page = curl_exec($ch);
   curl_close ($ch);

?>
I'm not sure if I understand the question though... is the problem combining SSL and multipart files?

Posted: Thu Sep 15, 2005 4:55 pm
by Burrito
interestin'...I've neresent th' POSTFIELDS as an array. gonna give that a try right now...

I've always jus' created a strin' an' separated th' vars/values by ampersands. I'll let ye know how 't goes shortly

thx,

Burrito

Posted: Thu Sep 15, 2005 6:40 pm
by Burrito
't didna work....an' I verfied on th' manual CURLOPT_POSTFIELDS needs a string.

that sample ye provided be also wrought wi' a ton o' other errors...maybe php worked that way in 2002, but nay longer.

any other ideas?