Setup a simple cURL connection to POST data using PHP

cURL is used to send "POST" data from one web page to another behind the scenes.

If cURL is enabled on your server, it is fairly easy to setup. I had trouble finding a page that broke setting up a cURL connection down to its most simplest form by showing a cURL example, so I decided to post a page for it.

Example: cURL setup with PHP

<?php
 
//set up a connection variable for the page you will post data to
$curl_connection = curl_init('https://fundraising-matrix.com/matrix/leadsubmit.php');
 
//curl basic setup
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
 
//$_POST variables to pass
$post_items[] = 'firstname='.strip_tags($_POST['firstname']);
$post_items[] = 'lastname='.strip_tags($_POST['lastname']);
$post_items[] = 'item='.strip_tags($_POST['item_id']);
 
//format the $post_items into a string
$post_string = implode ('&', $post_items);
 
//send the $_POST data to the new page
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($curl_connection);
curl_close($curl_connection);
?>

Tags: