possible to send multipart form data with cURL and add files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

possible to send multipart form data with cURL and add files

Post 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>
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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?
Post Reply