[wp-hackers] POSTing a file via wp_remote_post

Dion Hulse (dd32) wordpress at dd32.id.au
Fri Jan 11 12:19:13 UTC 2013


Short story: WP_HTTP Doesn't handle posting of files.

You may be able to do it by building the HTTP Payload yourself and
passing it as a string to the 'body' param:
wp_remote_post( $url, array(
   'body' => $payload
) );

You'd need to somehow manage to get the multipart/form-data
Content-Type working, I'm not sure how easy that would be to achieve
using only the body parameter though.

I'd pull up Wireshark and grab a HTTP payload to see if it can be
done, but I don't have access to it right now..

On 11 January 2013 23:10, David Anderson <david at wordshell.net> wrote:
>> The various parts there need to be converted into their HTTP
>> representations. The OAuth parts should be parsed by an OAuth library
>> into an Authorization header.
>
> The DropBox PHP toolkit (https://github.com/benthedesigner/dropbox) is
> already doing that bit for me.
>
> I'm just writing the HTTP consumer. The toolkit calls into my function with
> a URL and a number of POST fields. All I need to do in my function is
> perform the HTTP POST operation.
>
>
>> Something like this would probably work:
>>
>>         $auth_header = ''; // use OAuth library here to generate this
>>         $response =
>>
>> wp_remote_post('https://api-content.dropbox.com/1/files/.../...?overwrite=1',
>> array(
>>                 'stream' => '/path/to/myfile.zip',
>>                 'headers' => array(
>>                         'Authorization' => $auth_header,
>>                         'Content-Disposition' => 'attachment;
>> filename=myfolder/myfile.zip'
>>                 ),
>>         );
>
> The toolkit is expecting my consumer to POST all the values it sends, as the
> body. So I can't use the above method, because I'm not just POST-ing the
> file; I'm POST-ing a number of parameters, and the file is one. Curl handles
> this by a special parameter. But I need to be able to stream the file as
> *part* (but not the whole) of a stream, using wp_remote_post... I suppose
> one thing I could do is create a new file, but that'll bring me other
> problems (I'm dealing with potentially huge files in a plugin that may be
> used by people in all kinds of situations).
>
> David
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list