GET and POST with cURL together

Sometimes it is necessary  to send data with cURL using both GET and POST data, a simple example:

	$name = 'John';
$surname = 'Smith';
// will be sent via POST:
$postData = array(
'name' => $name,
'surname' => $surname
);

// will be sent via GET as parameter string
$getData = '?city=London&param=1&test=22';
$url = 'http://myaddress.com' . $getData; //address
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
if(curl_errno($curl)){
throw new Exception(curl_error($curl));
}
curl_close($curl);

$postData - a  data array to be sent via POST

$getData -  GET string with parameters

Comments

No comments yet, you can leave yours: