问题
i need to send raw multipart data with a php POST but without an html form... im starting the process with jquery $.post() instead (the objective is to change a twitter account's background).
How can i achieve that? This is my current (and still incomplete) code:
1) Image filename is inserted in this hidden input field:
<input type="hidden" id="profile_background_image_url" value="oats.jpg" />
2) when clicking on the submit button, a javascript function is triggered... and it calls:
$.post('helper.php',{
profile_background_image_url:$('#profile_background_image_url').val()
});
3) helper.php has
$param = array();
$param['image'] = '/www/uploads/'.$_POST['profile_use_background_image'];
$status = $connection->post('account/update_profile_background_image',$param);
Notes:
- all the background files are inside the /www/uploads local directory.
- im using Abraham Williams' twitteroauth library 0.2
Bottom line, in step three i need to send $param['image'] in raw multipart data to the $connection object (twitter library).
Any ideas?
Some references: http://dev.twitter.com/doc/post/account/update_profile_background_image
回答1:
Yeah i see now that hes building the post fields array into a query string which means you have to manually set the content type and that the @ key in the image fields wont do its magic since that only works with an array argument. More importantly i dont see a way to modify the headers without hacking the library or extending it and replacing certain functions.
I would try would be prepending @ to the file path of the image param like:
$param['image'] = '@/www/uploads/'.$_POST['profile_use_background_image'];
That is the convenient way to do it with cURL, and it looks like the libray basically uses cURL to make the request, so that should work.
回答2:
solved!
curl_setopt($ci, CURLOPT_POST, TRUE);
if(is_array($files)){
$post_file_array = array();
foreach($files as $key=>$value){
$post_file_array[$key] = "@{$value}";
}
curl_setopt($ci, CURLOPT_POSTFIELDS, $post_file_array);
if (!empty($postfields)) {
$url = "{$url}?{$postfields}";
}
}
else if (!empty($postfields)) {
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
}
来源:https://stackoverflow.com/questions/4372823/sending-raw-multipart-data-through-jquery-post-and-php