// Assume we are POSTing data from the variable $data.
$http_header = array(
"Content-Type: application/x-www-form-urlencoded" . "\r\n" .
"Content-Length: ". strlen($data) . "\r\n" .
...
"Custome-Header-Field: hello" . "\r\n" .
"Required-Empty-Header-Field:" . "\r\n" .
...
"Connection: Keep-Alive",
"Other-Value-One: world",
"Other-Value-Two: thisworks");
// Add optional headers.
if (!empty($some_condition)) {
array_push($http_header, "Other-Value-Three: whatever");
}
// Set up curl_exec.
$curl = curl_init();
$url = "https://www.somewhere-over-the-rainbow.com";
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeader);
$response = curl_exec($curl);
curl_close($curl);