December 19, 2023 5:01 am

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.

[wpdm_file id=5]

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);

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

5 responses to “HTTP POST Using PHP cURL”

  1. So Kea says:

    hi, do you know how to preview html in pdf

  2. Itx Boss says:

    can you please create an video on cURL!! Please create a video on cURL introduce it and show its difference commands and uses

  3. Huzoor Bux says:

    That page not available please try any other page.

Leave a Reply

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