Submit a Form Using PHP and cURL

There are situations when you want to send data using POST to a URL, either local or remote. Why would you want to do this? Probably you want to submit data to an opt-in form, but without taking a valuable visitor away from your site. Or maybe you want to send data to several applications for various purposes, which would be impossible to do in the usual manner. So how can we deal with this problem?

Simulate submitting a form using cURL

cURL stands for Client URL, and it is a library of functions that can be used to connect through a wide range of protocols, such as HTTP, FTP, telnet and so on. cURL also speaks HTTPS, so it can be used to communicate with secure servers.

What we are going to use is, cURL HTTP. cURL supports POST and GET methods, file uploads, cookies, user/password authentications, even using proxy servers for connecting.

It can literally be used to programmatically simulate browsing behavior. It can connect to a remote site, login by posting username and password to the login form or by using HTTP authentication, then retrieve pages or upload files. All of this using pure PHP code.

Use cURL to post data

Begin by creating a new connection.

$curl_connection = curl_init('http://www.domainname.com/target_url.php');

A new connection is created using curl_init() function, which takes the target URL as parameter (the URL where we want to post our data). The target URL is same as the “action” parameters of a normal form, which would look like this:

<form method="post" action="http://www.domainname.com/target_url.php">

Now let’s set some options for our connection. We can do this using the curl_setopt() function. Go to curl_setopt() reference page for more information on curl_setopt() and a complete list of options.

curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-us) AppleWebKit Version/5.0 Safari/533.16");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

First, we set the connection timeout to 30 seconds, so we don’t have our script waiting indefinitely if the remote server fails to respond.

Then we set how cURL will identify itself to the remote server. Some servers will return different content for different browsers (or agents, such as spiders of the search engines), so we want our request to look like it is coming from a popular browser.

CURLOPT_RETURNTRANSFER set to true forces cURL not to display the output of the request, but return it as a string.

Set CURLOPT_SSL_VERIFYPEER option to false, so the request will not trigger an error in case of an invalid, expired or not signed SSL certificate.

Finally, set CURLOPT_FOLLOWLOCATION to 1 to instruct cURL to follow "Location: " redirects found in the headers sent by the remote site.

Now prepare the data that we want to post. First store this in an array, with the key of an element being the same as the input name of a regular form, and the value being the value to post for that field.

For example, if in a regular form was:

<input type="text" name="firstName" value="Name">
<input type="hidden" name="action" value="Register">

add this to our array like this:

$post_data['firstName'] = 'Name';
$post_data['action'] = 'Register';

Data will be posted in the following format:

key1=value1&key2=value2

In order to format the data like this, create strings for each key-value pair (for example: key1=value1), put them in another array ($post_items) then combine them in one string using PHP function implode():

foreach ($post_data as $key => $value) 
{
    $post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);

Next, tell cURL which string is being posted. For this, use the CURLOPT_POSTFIELDS option.

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

Finally, execute the post, then close the connection.

$result = curl_exec($curl_connection);
curl_close($curl_connection);

By now, the data should have been posted to the remote URL. Go check this, and if it did not work properly, use curl_getinfo() function to see any errors that might have occurred.

print_r(curl_getinfo($curl_connection));

This line displays an array of information regarding the transfer. This must be used before closing the connection with curl_close();.

It is possible to see number and description of the error by outputting curl_errno($curl_connection) and curl_error($curl_connection).

Here is the complete code:

<?php

//create array of data to be posted
$post_data['firstName'] = 'Name';
$post_data['action'] = 'Register';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value)
{
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection = curl_init('http://www.domainname.com/target_url.php');

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, 
 "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-us) AppleWebKit Version/5.0 Safari/533.16");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' . curl_error($curl_connection);

//close the connection
curl_close($curl_connection);
?>

Post form data without using cURL

If your hosting server does not come with cURL installed (though this is rare as cURL is installed on most commercial hosting servers) and you also don’t have access to server in order to install it, there are alternatives.

One of them is using PHP functions fsockopen() and fputs() to send properly formatted data to a remote server. Here is a sample of code that does just this:

<?php

//create array of data to be posted
$post_data['firstName'] = 'Name';
$post_data['action'] = 'Register';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value)
{
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//we also need to add a question mark at the beginning of the string
$post_string = '?' . $post_string;

//we are going to need the length of the data string
$data_length = strlen($post_string);

//let's open the connection
$connection = fsockopen('www.domainname.com', 80); 

//sending the data
fputs($connection, "POST /target_url.php HTTP/1.1\r\n"); 
fputs($connection, "Host: www.domainname.com \r\n"); 
fputs($connection, "Content-Type: application/x-www-form-urlencoded\r\n"); 
fputs($connection, "Content-Length: $data_length\r\n"); 
fputs($connection, "Connection: close\r\n\r\n"); 
fputs($connection, $post_string); 

//closing the connection
fclose($fp);

?>
No comments yet.

Leave a Reply

revia in alcoholism