HTTP POST Using PHP cURL
Php 05-Nov-2019

HTTP POST Using PHP cURL

In this tutorial we have a simple example on how to use the cURL to POST data to a website.

PHP Code

Simple PHP Code

//extract data from the post
extract($_POST);

//set POST variables
$url = 'http://www.example.com/get-post.php';
$fields = array(
            'l_Name'=>urlencode($l_Name),
            'f_Name'=>urlencode($f_Name),
            'company'=>urlencode($company),
            'age'=>urlencode($age),
            'email'=>urlencode($email)
        );

foreach($fields as $key=>$value) 
{
     $string_Fields .= $key.'='.$value.'&'; 
}
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);