www.webdeveloper.com
Results 1 to 10 of 10

Thread: cURL Request in diff methods

  1. #1
    Join Date
    Sep 2006
    Posts
    655

    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. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,324
    Easiest thing probably would be to look for a 3rd party PHP class or function for sending REST (or RESTFUL) requests.

    If you want to write it yourself, you'll need to use different curl_setopt() options to specify the HTTP request type. You'll also need conditional logic to set the post fields if using post, otherwise putting them into the query string of the URL (could be a good case for using a switch()).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2006
    Posts
    655
    Hi

    Is there any difference between REST and RESTFUL requests?

    Thanks

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,324
    Just a short name and a longer name for the same thing.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Sep 2006
    Posts
    655
    oh ok thanks

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,324
    PS: You'll be looking for a RESTful client in this case, not a server (or something that has both, from which you'll just need the client).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Sep 2006
    Posts
    655
    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

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,324
    I'm guessing that this line overrides the setting and forces it to use POST:
    PHP Code:
    curl_setopt($chCURLOPT_POSTFIELDS,$curlData); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Sep 2006
    Posts
    655
    Yes but what should we use instead of that?

    Thanks

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,324
    If it's not a POST request, then you need to send and data via the URL query string instead of as POST data. See http_build_query() for help in doing that.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles