ixudra / curl

Custom PHP curl library for the Laravel 5 framework - developed by Ixudra
MIT License
561 stars 128 forks source link

Update curl file upload to use new PHP7 CurlFile feature #22

Closed elimentz closed 7 years ago

elimentz commented 8 years ago

PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax See: https://wiki.php.net/rfc/curl-file-upload for details

function getCurlFileValue($filename, $contentType, $postname) 
{
    if (function_exists('curl_file_create')) {
        return curl_file_create($filename, $contentType, $postname);
    }
    // Use the old style if using an older version of PHP
    $value = "@{$filename};filename=" . $postname;
    if ($contentType) {
        $value .= ';type=' . $contentType;
    }
    return $value;
}
elimentz commented 7 years ago

Done in v 6.12.0, see documentation for details:

For sending files via a POST request, you can use the withFile method to correctly format a request before sending:


    use Ixudra\Curl\Facades\Curl;

    $response = Curl::to('http://foo.com/bar')
        ->withData( array( 'Foo' => 'Bar' ) )
        ->withFile( 'image_1', '/path/to/dir/image1.png', 'image/png', 'imageName1.png' )
        ->withFile( 'image_2', '/path/to/dir/image2.png', 'image/png', 'imageName2.png' )
        ->post();

You can add as many files to the request as you want. A couple of things to keep in mind: