Skip to content

Instantly share code, notes, and snippets.

@recrack
Forked from iansltx/MultiPartFromStrings.php
Created July 21, 2019 07:52
Show Gist options
  • Save recrack/f25436c6f3c0492e38d0740f18e5e40e to your computer and use it in GitHub Desktop.
Save recrack/f25436c6f3c0492e38d0740f18e5e40e to your computer and use it in GitHub Desktop.

Revisions

  1. @iansltx iansltx renamed this gist May 2, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @iansltx iansltx created this gist May 2, 2015.
    54 changes: 54 additions & 0 deletions function.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    <?php

    /**
    * PHP's curl extension won't let you pass in strings as multipart file upload bodies; you
    * have to direct it at an existing file (either with deprecated @ syntax or the CURLFile
    * type). You can use php://temp to get around this for one file, but if you want to upload
    * multiple files then you've got a bit more work.
    *
    * This function manually constructs the multipart request body from strings and injects it
    * into the supplied curl handle, with no need to touch the file system.
    *
    * @param $ch resource curl handle
    * @param $boundary string a unique string to use for the each multipart boundary
    * @param $fields string[] fields to be sent as fields rather than files, as key-value pairs
    * @param $files string[] fields to be sent as files, as key-value pairs
    * @return resource the curl handle with request body, and content type set
    * @see http://stackoverflow.com/a/3086055/2476827 was what I used as the basis for this
    **/
    function buildMultiPartRequest($ch, $boundary, $fields, $files) {
    $delimiter = '-------------' . $boundary;
    $data = '';

    foreach ($fields as $name => $content) {
    $data .= "--" . $delimiter . "\r\n"
    . 'Content-Disposition: form-data; name="' . $name . "\"\r\n\r\n"
    . $content . "\r\n";
    }
    foreach ($files as $name => $content) {
    $data .= "--" . $delimiter . "\r\n"
    . 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . "\r\n\r\n"
    . $content . "\r\n";
    }

    $data .= "--" . $delimiter . "--\r\n";

    curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
    'Content-Type: multipart/form-data; boundary=' . $delimiter,
    'Content-Length: ' . strlen($data)
    ],
    CURLOPT_POSTFIELDS => $data
    ]);

    return $ch;
    }

    // and here's how you'd use it

    $ch = curl_init('http://httpbin.org/post');
    $ch = buildMultiPartRequest($ch, uniqid(),
    ['key' => 'value', 'key2' => 'value2'], ['somefile' => 'contents!', 'someotherfile' => 'yoloswag']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    echo curl_exec($ch);