0

I am trying to make a script that sends data through PUT. However I seem not to succeed.

I have tried to do so with a CURL script but to no avail.

        ///######## INDICATE PUT
        curl_setopt($this->CurlOBJ, CURLOPT_CUSTOMREQUEST, 'PUT');

I think I need to do more however generally speaking what I can find on the internet is that I am supposed to this by :

$data = file_get_contents('php://input')

I have however no clue how to send data with file_get_contents(); Furthermore I am asked to send a header with an API key.

However my second question is how to send a header key with PUT. Third is... Is using PUT a good idea for building an API. This however is a side question.

So how can I send data by PUT and getting any valid return?

Answer

Tadman has send me in the right direction! So even though this question had been marked as a duplicate. Still his answer gave me the solution.

For those like me that didn't have enough with the first answer hereby my piece of working code.

Thanks!

<?php

$bookingID = 'aae2cdfd-6789-474d-828c-190e14a0f01e';
$url = 'https://api.someapp.com/application/'.$bookingID.'/update';

$data = array(
                'changeDate'  => date('c'),
                'options'       => array(
                                            'allowEdit' =>  true
                                        )
            );

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,    http_build_query($data));

///######### SET THE HEADER
$headers = array('apiSetKey: 2388089');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);


$response = curl_exec($ch);

if (!$response)
{
    return false;
}
else {
    ///######## JSON DECODE DATA
    $response = json_decode($response, true);

    print_r($response);
}

?>
Alex
  • 1,223
  • 1
  • 19
  • 31
  • `PUT` is just a REST thing. Also if you have no clue about how something works the first step is to read the documentation, then experiment, and finally when you have code that's not working, to ask here. This looks like it's premature, you don't really have anything people can work with. – tadman Jul 06 '17 at 16:23
  • @tadman `PUT` is actually an HTTP thing. – Alex Blex Jul 06 '17 at 17:10
  • @tadman, despite of what you think. your comment actually helped me a lot further! The only question still remaining is how to send headers. – Alex Jul 06 '17 at 17:23
  • @AlexBlex You're right, it's part of the HTTP standard, but it was REST that popularized it. Conventional HTTP makes use of POST except when talking about things like WebDAV. – tadman Jul 06 '17 at 17:53
  • Yeah, that looks good. A little clean-up could help, but can't argue with functional! – tadman Jul 07 '17 at 21:30

0 Answers0