[wp-hackers] POSTing a file via wp_remote_post

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


Got bored enough to try it without a Tcpdump.. The following code will
work, but you'll have to modify it to suit your purposes
(specifically, moving the if ( $local_file) into the parent block or
something) as it's an example only, it won't do exactly what you want:


$local_file = dirname(__FILE__) .'/myfile.png';

$post_fields = array(
        'one' => 'the number one',
        'submit' => 'Upload a file'
);

$boundary = wp_generate_password(24); // Just a random string, use
something better than wp_generate_password() though.
$headers = array(
        'content-type' => 'multipart/form-data; boundary=' . $boundary
);

$payload = '';

// First, add the standard POST fields:
foreach ( $post_fields as $name => $value ) {
        $payload .= '--' . $boundary;
        $payload .= "\r\n";
        $payload .= 'Content-Disposition: form-data; name="' . $name .
'"' . "\r\n\r\n";
        $payload .= $value;
        $payload .= "\r\n";
}
// Upload the file
if ( $local_file ) {
        $payload .= '--' . $boundary;
        $payload .= "\r\n";
        $payload .= 'Content-Disposition: form-data; name="' . $name .
'"; filename="' . basename( $local_file ) . '"' . "\r\n";
//        $payload .= 'Content-Type: image/jpeg' . "\r\n"; // If you
know the mime-type
        $payload .= "\r\n";
        $payload .= file_get_contents( $local_file );
        $payload .= "\r\n";
}

$payload .= '--' . $boundary . '--';

$res = wp_remote_post( 'http://test.server/upload.php', array(
        'headers' => $headers,
        'body' => $payload,
) );

A certain someone will pipe up saying this isn't efficient for large
files, and it isn't, but it'll certainly work if you want to stay
within the current constraints of WP_HTTP / wp_remote_post().

On 11 January 2013 23:19, Dion Hulse (dd32) <wordpress at dd32.id.au> wrote:
> 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