In this article am going to explain you how to make HTTP POST using PHP cURL. Some places you may need to post data to remote server where the data will insert to database or process and send you the response. Mostly APIs will use these methods. Apart from API we require it to post the data to remote server from local or for cross domain request.
User will make a conventional form submission and we need to get those values and create an cURL request. In this method we send a set of (array) data and remote script will insert to database (MYSQL Insert) or some other data process.
PHP cURL Post
Now we are going to call the set of php cURL function to post data,
//Initialize cURL $ch = curl_init(); //Set location to post data using cURL curl_setopt($ch, CURLOPT_URL,"http://www.your-website.com/yourScript.php"); //Enable POST method curl_setopt($ch, CURLOPT_POST, 1); //Create a array of data curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'name'=>'your name', 'email'=>'your email', 'mobile'=>'your mobile', 'dob'=>'your date of birth', 'location'=>'your location' )); //Set return True curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Execute the cURL echo $server_output = curl_exec ($ch); //Close curl_close ($ch);
The above script sends a post a HTTP post request to the given URL with the array of data. Remote script will receive this data and insert into database or process this according to the requirement. Hope you like this article. Thank you.