Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    892
    Thanks
    301
    Thanked 2 Times in 2 Posts

    Question cURL Request in diff methods

    Hi

    I want to send some data via cURL using the following methods:

    • GET
    • POST
    • PUT
    • DELETE


    I got the following code from somewhere but I am not sure what to change in it to make it send data using the above HTTP method.

    Code:
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
                curl_setopt($ch, CURLOPT_POST,1);
                curl_setopt($ch, CURLOPT_POSTFIELDS,$curlData);
                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.
                curl_setopt($ch, CURLOPT_URL, 'http://example.com');

    Thanks for any help

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,089
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by cancer10 View Post
    • PUT
    • DELETE
    You do realise that most webservers don't support these two methods don't you? - It's a security thing. If a webserver took these commands from anyone anywhere then any website could have its content deleted and replaced.

    Likewise if you're planning to use cURL to use these two methods on servers that are not yours, you won't get far.

    Why do you need to use them?
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #3

  • #4
    New to the CF scene
    Join Date
    May 2013
    Location
    India
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi, i am not much more familiar with cURL but i found a piece of code which send data with POST method using cURL in PHP.

    <?php
    $urltopost = "http://somewebsite.com/script.php";
    $datatopost = array (
    "firstname" => "Mike",
    "lastname" => "Lopez",
    "email" => "my@email.com",
    );

    $ch = curl_init ($urltopost);
    curl_setopt ($ch, CURLOPT_POST, true);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    $returndata = curl_exec ($ch);
    ?>

    Hope it will help you. Whether you want more detail, you may visit askapache.com/php/sending-post-form-data-php-curl.html

  • #5
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    892
    Thanks
    301
    Thanked 2 Times in 2 Posts
    I am trying to send data in 4 ways but getting the correct data for POST method.

    Here is my php code with curl request:
    Code:
    $data = array('data' => '1');
    	$curlData = '';
        foreach($data as $key=>$value){
                    $curlData .= $value['attribute'] . '=' . urlencode($value['value']) . '&';
    
        }
    
    	switch($methodName){
    		case 'GET':
    		    $curlParamName = 'CURLOPT_HTTPGET';
    		    $curlParamValue = 1;
    		    break;
    		case 'POST':
    		    $curlParamName = 'CURLOPT_POST';
    		    $curlParamValue = 1;
    		    break;
    		case 'PUT':
    		    $curlParamName = 'CURLOPT_PUT';
    		    $curlParamValue = 1;
    		    break;
    		case 'DELETE':
    		    $curlParamName = 'CURLOPT_CUSTOMREQUEST';
    		    $curlParamValue = 'DELETE';
    		    break;
    
    	}
    
    
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    	curl_setopt($ch, $curlParamName, $curlParamValue );
    	curl_setopt($ch, CURLOPT_POSTFIELDS,$curlData);
    	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.
    	curl_setopt($ch, CURLOPT_URL, 'http://localhost/api.php');
    	$data = curl_exec($ch);
    	curl_close($ch);
    print_r($data);

    For 'GET', I am getting response:
    Method POST
    Data 1

    For 'POST', I am getting response:
    Method POST
    Data 1

    For 'PUT', I am getting response:
    Method POST
    Data 1

    For 'DELETE', I am getting response:
    Method POST
    Data 1


    Content of api.php file
    Code:
    <?php
    echo 'Method '. $_SERVER['REQUEST_METHOD'];
    echo '<br>';
    echo 'Data '. $_REQUEST['data'];
    die;
    ?>
    Can someone please tell me why is the "Method = POST" for every case?


    Thanks

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,089
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by cancer10 View Post
    For 'GET', I am getting response:
    Method POST
    Data 1

    For 'POST', I am getting response:
    Method POST
    Data 1

    For 'PUT', I am getting response:
    Method POST
    Data 1

    For 'DELETE', I am getting response:
    Method POST
    Data 1


    Can someone please tell me why is the "Method = POST" for every case?
    I think I covered it with:

    Quote Originally Posted by tangoforce View Post
    You do realise that most webservers don't support these two methods don't you? - It's a security thing.
    Thnk about it logically though, for put and delete requests I would expect the parameters to be sent in the headers like post because otherwise it would be a get request so this behaviour may actually be correct.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #7
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    892
    Thanks
    301
    Thanked 2 Times in 2 Posts
    I understand that for PUT and DELETE, but what about GET, why does it show POST when the method is GET?

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,089
    Thanks
    51
    Thanked 506 Times in 493 Posts
    CURLOPT_HTTPGET TRUE to reset the HTTP request method to GET. Since GET is the default, this is only necessary if the request method has been changed.
    In other words, don't bother using this option in the GET part of your switch. Try dropping it and see what happens.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #9
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    892
    Thanks
    301
    Thanked 2 Times in 2 Posts
    ok thanks.

    Can someone please confirm me if the method PUT is used ONLY for uploading files and NOT for string data?

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,089
    Thanks
    51
    Thanked 506 Times in 493 Posts
    I doubt anyone here will confirm that. As I've said to you most webservers don't support it so your odds of finding people here who know much about it are slim.

    I'm afraid you're going to have to start looking at the RFCs with regards to this.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • Users who have thanked tangoforce for this post:

    phantom007 (06-18-2013)

  • #11
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    892
    Thanks
    301
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by tangoforce View Post
    I doubt anyone here will confirm that. As I've said to you most webservers don't support it so your odds of finding people here who know much about it are slim.

    I'm afraid you're going to have to start looking at the RFCs with regards to this.
    Hi

    Can u please tell me how do I make sure that my webserver does not support PUT method?

    Because on my localhost when I send a request via PUT method and on the server when I echo $_SERVER['REQUEST_METHOD'], it displays PUT, so does that mean my localhost supports PUT method?

    Thanks


  •  

    Posting Permissions

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