Send file to API with PHP using cURL

10,568

There are 2 potential issues.

  1. To POST a file using cURL, it must be located on the local filesystem. You can't specify a file over http://.
  2. Depending on your PHP version you may need to use the CURLFile class instead of prepending the filename with an @.

Here is some code that fixes both issues:

<?php

ini_set('display_errors', 1);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.noelshack.com/api.php');
curl_setopt($curl, CURLOPT_VERBOSE, 1);
//curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0');
//curl_setopt($curl, CURLOPT_ENCODING, '');
//curl_setopt($curl, CURLOPT_REFERER, 'http://www.noelshack.com/api.php');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

// download image to temp file for upload
$tmp = tempnam(sys_get_temp_dir(), 'php');
file_put_contents($tmp, file_get_contents('http://cdn.soccerwiki.org/images/player/2386.jpg'));

// send a file
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt(
    $curl,
    CURLOPT_POSTFIELDS,
    array(
      'fichier' => new CURLFile($tmp),
      'submit'  => 'Envoyer',
    ));

// output the response
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
var_dump($result, curl_error($curl));

unlink($tmp); // remove temp file

curl_close($curl);

If you don't have PHP 5.5.0 or greater, just remove the CURLFile construction and replace it with '@' . $tmp,

Share:
10,568

Related videos on Youtube

Thomas L.
Author by

Thomas L.

I'm just a random student in computer science and mathematics engineering school called Ensimag, in Grenoble (France)

Updated on June 04, 2022

Comments

  • Thomas L.
    Thomas L. almost 2 years

    I am trying to upload a file to this API : http://www.noelshack.com/api.php I don't know anything about cURL. I have find sample codes, and i have try this :

    <?php
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, 'http://www.noelshack.com/api.php');
    
    // send a file
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt(
        $curl,
        CURLOPT_POSTFIELDS,
        array(
          'fichier' => '@http://cdn.soccerwiki.org/images/player/2386.jpg'
        ));
    
    // output the response
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($curl);
    print_r($result);
    
    curl_close($curl);
    
    ?>
    

    Can anyone explain me with my current code don't work ? It should return a url like this : http://www.noelshack.com/2016-XX-XXXXXXXXXX-2386.png

    But it didn't return anything. There aren't any error in my code, the file is juste not uploaded, and i don't know why because i don't know how to display api errors !

    Thanks in advance

    (sorry for my english, i'm french)

    • Thomas L.
      Thomas L. almost 8 years
      i have add this code to see if curl return an error : if(curl_exec($curl) === false) { echo 'curl error : '.curl_error($curl); } else { echo "no problem"; } and it returns "no problem" !
  • Harish Kumar
    Harish Kumar almost 7 years
    Hi @drew010 I am working with php 7.1.1 and trying to upload image with CURL. But not succeed here is my code <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'someurl'); $filename = $_FILES['image']['name']; $filedata = $_FILES['image']['tmp_name']; $filesize = $_FILES['image']['size']; $filetype = $_FILES['image']['type']; $cFile = new CURLFile($filedata, $filetype, 'harish'); $data = array('upload-file' => $cFile); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, $sheader); curl_exec($ch); ?>