4

First of all I'm working based on the following assumption: according to the REST architecture you can use PUT to create a new resource, in my case a file with additional informations provided by the user.

If this concept is not correct please let me know so I don't ask an incorrect question from the architectural point of view.

I see there are two things related to PUT request using CURL.

With the following method you can send an array of values just like a normal POST request.

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

and using this:

curl_setopt($ch, CURLOPT_PUT, 1);

a file can be uploaded.

  • Are this two options related ?
  • Are they complementarity ways to send both a file and some meta information in the same PUT request?
  • What is the solution to upload a file and send additional information for it like category and description

    I'm just trying to mimic the POST functionality

        $post_params['name'] = urlencode('Test User');
        $post_params['file'] = '@'.'/tmp/never_ending_progress_bar2.gif';
    
  • johnlemon
    • 20,761
    • 42
    • 119
    • 178

    1 Answers1

    6

    CURLOPT_CUSTOMREQUEST is useful when you want / need to do some kind of special request that is not common enough to be supported by itself, via its own option.

    CURLOPT_POST, CURLOPT_PUT, and CURLOPT_GET allow you to send POST / PUT / GET requests -- which are some types of requests that are common enough to have their own options ; which means they don't need you to use CURLOPT_CUSTOMREQUEST.

    Mike Cialowicz
    • 9,892
    • 9
    • 47
    • 76
    Pascal MARTIN
    • 395,085
    • 80
    • 655
    • 663
    • How can you send something else then a file using CURLOPT_PUT ? – johnlemon Mar 25 '11 at 08:09
    • 1
      `$file = tmpfile(); fwrite($file, $yourString); fseek($file, 0); curl_setopt($curl, CURLOPT_INFILE, $file); curl_setopt($curl, CURLOPT_INFILESIZE, strlen($yourString));` – Slava Mar 25 '11 at 08:32
    • Correct. But I need to send both a file upload and some extra info. Is that possible ? – johnlemon Mar 25 '11 at 08:33
    • Well, there are options: you can set any GET parameters like for any normal GET request; you can use request headers; you can encode everything in file body, like making it an XML document or JSON object containing your file AND extra fields, just make sure remote server will parse it correctly. – Slava Mar 25 '11 at 08:37
    • Using Get seems like perverting a good PUT request. Header is ok but from the project requirements I can't use this solution. Encoding everything seems an interesting solution. Maybe a bit disappointing because this seems it should work out of the box like the post request. – johnlemon Mar 25 '11 at 08:45