submit a form with post method to remote server using php curl
We learn how to submit the form data to remote server in my previous example with URL string.
Here we can pass data with POST multipart method,main difference is that we passs it as array.
To submit forms using cURL, we need to follow the below steps:
1. Prepare the data to be posted
2. Connect to the remote URL
3. Post (submit) the data
4. Fetch response and display it to the user or do some other stuff with it.
Below Example Has two files,one to post the data and other is to process the data.
/* * Data which is to submitted to the remote URL */ $post_array = array('fname'=>'mahesh','lname'=>'chari'); /* * Initialize cURL and connect to the remote URL * You will need to replace the URL with your own server's URL * or wherever you uploaded this script to. */ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://example.com/curlpost_handler.php' ); /* * Instruct cURL to do a regular HTTP POST */ curl_setopt($ch, CURLOPT_POST, TRUE); /* * Specify the data which is to be posted */ curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array); /* * Tell curl_exec to return the response output as a string */ curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); /** * Execute the cURL session */ $response = curl_exec($ch ); /** * Close cURL session and file */ curl_close($ch ); echo $response; // is Welcome Maheshchari.
Handling post data on curlpost_hanlder.php
$firstname=$_POST['fname']; $lastname=$_POST['lname']; echo "Welcome ".$firstname.$lastname;