SitePoint Sponsor

User Tag List

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

    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
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,386
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi cancer10,

    You can use these options to set the type of request:

    CURLOPT_CUSTOMREQUEST : A custom request method to use instead of "GET" or "HEAD" when doing a HTTP request. This is useful for doing "DELETE" or other, more obscure HTTP requests.
    CURLOPT_HTTPGET : TRUE to reset the HTTP request method to GET.
    CURLOPT_POST : TRUE to do a regular HTTP POST.
    CURLOPT_PUT : TRUE to HTTP PUT a file. The file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE.

    (source: http://stackoverflow.com/questions/2...st-type-in-php)

  3. #3
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    736
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,386
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi cancer10,

    Sorry, my first reply was a bit rushed and I can see it wasn't that helpful. I've made some changes to your code to get it working:
    PHP Code:
    $data = array('data' => '1');
    $curlData http_build_query($data); //Handy function provided by php saves formatting the POST data yourself

    $url "http://localhost/api.php"//I've set a variable for the URL, so we can append a query string if we want to use GET
    $ch curl_init();

    switch(
    $methodName){
        case 
    'GET':
            
    curl_setopt($chCURLOPT_HTTPGETtrue);
            
    $url .= '?' $curlData;
            
    $curlData false// See the IF check below 
            
    break;
        case 
    'POST':
            
    curl_setopt($chCURLOPT_POSTtrue);
            break;
        case 
    'PUT':
            
    curl_setopt($chCURLOPT_PUTtrue);
            break;
        default:
            
    curl_setopt($chCURLOPT_CUSTOMREQUEST$method);
    }

    if (
    $curlData) { 
        
    curl_setopt($chCURLOPT_POSTFIELDS$curlData); //You don't wanna set this option if you want to make a GET request, otherwise you'll get POST
    }

    curl_setopt($chCURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($chCURLOPT_HEADER0);
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
    curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
    curl_setopt($chCURLOPT_RETURNTRANSFER1); //Set curl to return the data instead of printing it to the browser.
    curl_setopt($chCURLOPT_URL$url);
    $data curl_exec($ch);
    curl_close($ch);
    print_r($data); 
    There are some wrappers out there for curl which can simplify a lot of this stuff.. this one for example: https://github.com/shuber/curl, then your request code becomes as simple as:
    PHP Code:
    $response $curl->get($url$data);
    $response $curl->post($url$data);
    //etc. 


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
  •