Jump to content

cURL multi-part form posting


freeloader

Recommended Posts

Hey guys,

 

Recently I bumped into a little coding problem. I'm trying to submit information to a multi-part form (including an image upload). The receiving server couldn't read my input, figured I had a nill object somewhere (RoR). First I thought the problem was in my curl setopts, but I think they are fine. It looks like the problem is with the data I'm submitting.

 

I read a bit about multi part posting, and every article I read advised to make a postfield array, which curl would interpret and send out correctly. I did and this was my resulting array:

 

$postfield = array ("social[comments]" => "Test", "avatar_file" => "", "friend_avatar_file" => "", "forum_avatar_file" => "", "social[signature]" => "", "forum_signature_file" => "", "save_profile.x" => "25", "save_profile.y" => "9");

 

That failed. So I tried making it into a URLENCODED string:

 

$postfield = "social%5Bcomments%5D=".stripslashes($_POST['textarea'])."&avatar_file=&friend_avatar_file=&forum_avatar_file=".$file."&social%5Bsignature%5D=&forum_signature_file=&save_profile.x=25&save_profile.y=9";

 

This finally worked. I was able to submit the info I wanted. I'd like to send a file with it this time though. I'd need to put this into a postfield array to work though.

 

if(!$file = upload( )) { $file = ''; } else { $file = '@'.$file; }

function upload( ) {
if(isset($_FILES['file']) && !empty($_FILES['file']['name']))
{	
	$uploaddir = "temp";  

	if(is_uploaded_file($_FILES['file']['tmp_name'])) {
		move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
		$searchfile = $uploaddir.'/'.$_FILES['file']['name'];
		return $searchfile;	
	}
}
return false;
}

 

So, I got the uploading covered. I need to put generate the postfield array though. I have the feeling the problem is with the brackets [ ] I'm using. I tried urlencoding the keys in the array, but that didn't work either.

 

Does anybody have an idea on how to handle brackets in post (multi-part) data?

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/212539-curl-multi-part-form-posting/
Share on other sites

Update: ok, so I figured that running http_build_query( ) on the array makes me the right post fields. It doesn't fix my problem though, it allows me to submit the info I want using the array, but when including a file, the server still freaks (other end, not my end). So I'm still doing something wrong... Any help appreciated :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.