Hi
I want to send some data via cURL using the following methods:
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.
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
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/2153650/how-to-start-a-get-post-put-delete-request-and-judge-request-type-in-php)
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:
$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
<?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
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:
$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($ch, CURLOPT_HTTPGET, true);
$url .= '?' . $curlData;
$curlData = false; // See the IF check below
break;
case 'POST':
curl_setopt($ch, CURLOPT_POST, true);
break;
case 'PUT':
curl_setopt($ch, CURLOPT_PUT, true);
break;
default:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}
if ($curlData) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlData); //You don't wanna set this option if you want to make a GET request, otherwise you'll get POST
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
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, $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:
$response = $curl->get($url, $data);
$response = $curl->post($url, $data);
//etc.