Submitting form to remote server.
We learned some basics in my previous article cURL basics. Here we cover a topic how to submit a form to remote server.
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_str = urlencode("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_str); /* * 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; |

