[wp-trac] [WordPress Trac] #35388: Add support to wp_remote_request to support multipart/form-data

WordPress Trac noreply at wordpress.org
Sat Jan 9 20:34:44 UTC 2016


#35388: Add support to wp_remote_request to support multipart/form-data
-------------------------+-----------------------------
 Reporter:  pbearne      |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  HTTP API     |    Version:  trunk
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 I had to push a file using 'multipart/form-data' to an API and wanted to
 use internal the internal remote request code instead of a 3rd party
 library.

 I was able to with this code but its a bit messy so I would like to
 suggest that we add methods to wp_remote_request to support this e.g.
 PUT_MUTLI and POST_MULTI and payload is passed in the body arg if as an
 array or object, then we split the content into the elements of the
 multipart allowing the defaults to be overwritten

 I am posting this without a patch to make sure I patch along these lines
 would accepted?


 Thoughts?


 {{{#!php

 $local_file = $_FILES['resume']['tmp_name'];
                 // wp_remote_request way

 //https://github.com/jeckman/wpgplus/blob/master/gplus.php#L554
                 $boundary = md5( time() . $ext );
                 $payload  = '';
                 $payload .= '--' . $boundary;
                 $payload .= "\r\n";
                 $payload .= 'Content-Disposition: form-data;
 name="photo_upload_file_name"; filename="' . $_FILES['resume']['name'] .
 '"' . "\r\n";
                 $payload .= 'Content-Type: ' . $format . '\r\n'; // If you
 know the mime-type
                 $payload .= 'Content-Transfer-Encoding: binary' . "\r\n";
                 $payload .= "\r\n";
                 $payload .= file_get_contents( $local_file );
                 $payload .= "\r\n";
                 $payload .= '--' . $boundary . '--';
                 $payload .= "\r\n\r\n";

                 $args = array(
                         'method'  => 'PUT',
                         'headers' => array(
                                 'accept'       => 'application/json', //
 The API returns JSON
                                 'content-type' => 'multipart/form-
 data;boundary=' . $boundary, // Set content type to multipart/form-data
                         ),
                         'body'    => $payload,
                 );

                 $url      = add_query_arg(
                         array(

                                 'externalID'  => 'Portfolio',
                                 'fileType'    => 'SAMPLE',
                         ), self::$url . '/file/'
                 );
                 $response = wp_remote_request( $url, $args );

 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/35388>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list