Kong / unirest-php

Unirest in PHP: Simplified, lightweight HTTP client library.
http://unirest.io/php
MIT License
1.28k stars 328 forks source link

little forgetfulness in Body Class #135

Open m4ffucci opened 6 years ago

m4ffucci commented 6 years ago

I needed to send an image file with title and caption from a classic html form to a local php controller. I wanted to forward the form data to a remote webservice with Unirest, to do this I used the following method of the Body class.


 public static function Multipart($data, $files = false)
    {
        if (is_object($data)) {
            return get_object_vars($data);
        }
        if (!is_array($data)) {
            return array($data);
        }
        if ($files !== false) {
            foreach ($files as $name => $file) {
                $data[$name] = call_user_func(array(__CLASS__, 'File'), $file);
            }
        }
        return $data;
    }

The $files parameter is an array of $file, so $file can contain $filename, $mimetype and $postname.

The call_user_func method does not provide for the split of the array elements into individual parameters for Body::File().

I proceeded to fix it just like that:

# Original line
# -------------------------------------------------------------
# $data[$name] = call_user_func(array(__CLASS__, 'File'), $file);

# Fixed line
$data[$name] = call_user_func_array(array(__CLASS__, 'File'), $file);