Typical way to send CURL POST data in PHP

Here is a typical way to send PHP data to another script and have that data returned via CURL POST. Keep in mind there are a lot of server security restrictions that may impede CURL from working properly. Visit the PHP cURL book to learn more.


function curlPost ($url,$valsArr)
{
   //create name / value pairs
   foreach($valsArr as $key => $val)
   {
      $postDataArr[]= $key . ‘=’.$val;
   }

$postData=implode("&",$postDataArr);

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

$output=curl_exec($ch);

curl_close($ch);
return $output;

}

// Call function like so:

$valsArr = array(
   “sampleField1” => “value1”,
   “sampleField2” => “value2″,
   “sampleField3” => “value3”
);

echo curlPost(“http://www.prolificfutility.com/path-to-curlPost.php”,$valsArr);

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *