SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question REST Webservice: PUT & Delete methods

    Hi All

    I am using Guzzle PHP client to make REST calls for PUT and DELETE.

    On the server I have this code in my index.php file where the request is being made.

    PHP Code:
    //index.php
    echo $_SERVER['REQUEST_METHOD'], '<BR>';
    print_r($_REQUEST);
    print_r(getallheaders()) 
    For GET and POST methods, I am getting the values of all three variable/functions ie. $_SERVER['REQUEST_METHOD'], $_REQUEST and getallheaders(), but in case of PUT and DELETE methods I am only getting value of $_SERVER['REQUEST_METHOD'] i.e PUT or DELETE and just the data in the getallheaders(), but nothing in the $_REQUEST variable.

    What could be the reason?

    Any inputs will be great...


    Thanks

  2. #2
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,267
    Mentioned
    33 Post(s)
    Tagged
    5 Thread(s)
    Hi,

    The in PHP, the data for incoming PUT and DELETE requests is available via the php://input stream. You can get the contents of the stream using file_get_contents("php://input") but the data is returned as a query string, so you'll probably want to parse that out into separate variables:

    PHP Code:
    parse_str(file_get_contents("php://input"),$post_vars); 
    This will give you an array, $post_vars, with the request data.

  3. #3
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thanks for your reply.

    Yes, using your method I can now see some output at least....I have two queries:

    Following is the code I am using to make a REST request...The $data variable contains the data in array() format...

    For example:

    $data = array(
    'a' => 1,
    'b' => 2
    );

    Code:
    $ch = curl_init();
                   switch($methodName){
                        case 'PUT':
                            curl_setopt($ch, CURLOPT_URL, $apiUrl);
                            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
                            curl_setopt($ch, CURLOPT_POST, true);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                            curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT') );
                            break;
                        case 'DELETE':
                            curl_setopt($ch, CURLOPT_URL, $apiUrl);
                            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
                            curl_setopt($ch, CURLOPT_POST, true);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                            curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: DELETE') );
                            break;
    
                    }
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
                    
                    $data = curl_exec($ch);
                    curl_close($ch);

    Question 1) For both PUT and DELETE, When I use:

    Code:
    echo file_get_contents("php://input");
    I get the output:

    Code:
    ------------------------------42c0c61e0e16
    Content-Disposition: form-data; name="1"
    
    Array
    ------------------------------42c0c61e0e16
    Content-Disposition: form-data; name="2"
    
    Array
    ------------------------------42c0c61e0e16--

    and when I use:

    Code:
    parse_str(file_get_contents("php://input"),$post_vars);  
    
    print_r($post_vars);
    I get this output:

    Code:
    Array
    (
        [------------------------------d2bcc8532b06
    Content-Disposition:_form-data;_name] => "1"
    
    Array
    ------------------------------d2bcc8532b06
    Content-Disposition: form-data; name="2"
    
    Array
    ------------------------------d2bcc8532b06--
    
    )
    There is not much difference as I can see. Why?


    Question 2) In the above output, I can see just the values i.e. 1 and 2 and not the key i.e. 'a' and 'b'...why are they not included in the output?


    Thanks

  4. #4
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,267
    Mentioned
    33 Post(s)
    Tagged
    5 Thread(s)
    Try replacing this:
    PHP Code:
    curl_setopt($chCURLOPT_URL$apiUrl);
    curl_setopt($chCURLOPT_CUSTOMREQUEST'PUT');
    curl_setopt($chCURLOPT_POSTFIELDS$data);
    curl_setopt($chCURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT') ); 
    with this:
    PHP Code:
    curl_setopt($chCURLOPT_URL$apiUrl);
    curl_setopt($chCURLOPT_CUSTOMREQUEST'PUT');
    curl_setopt($chCURLOPT_POSTFIELDShttp_build_query($data)); 

  5. #5
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    works like a charm, thanks

  6. #6
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hi again

    What about getting data from HEAD method? How do we do that?

    Both file_get_contents("php://input") and $_REQUEST does not seem to output anything

    Thanks

  7. #7
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,267
    Mentioned
    33 Post(s)
    Tagged
    5 Thread(s)
    Hi,

    HEAD requests are indentical GET requests except that they don't return any response body. I've run a simple test on my machine and query string variables are available to PHP in the $_GET array as normal. How are you making the request?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •