Translator

Categories

Archives

How to submit a form using PHP

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 optin 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

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

What are we going to use are cURL HTTP capabilities. 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.

So how do I use cURL to post data?

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

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.

Then we 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, we set CURLOPT_FOLLOWLOCATION to 1 to instruct cURL to follow “Location:” redirects found in the headers sent by the remote site.

Now we must prepare the data that we want to post. We can 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 that we want to post for that field.

In order to format the data like this, we are going to 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() .

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 dont have access to server in order to install it, there are alternatives.

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

One question. How would I go about getting the returned data from the fsockopen method? I’m running it and not getting any errors, but nothing is being inserted into the database like it should and I’d like to see if there’s an error somewhere.

http://www.blue2blond.nl Erwin

@ Ryan

You could use fgets to retrieve the returned data. See the examples at http://php.net/fsockopen (search for fgets on that page).

http://footballfreetv.com footballfreetv

thanx man for this tutorial
u really saved my a$$..

thanx a bunch

Bob Jenkins

What if the web page has more than one form on it, how do you instruct cURL to use a specific form on the page? (And the assumption is that there are the same fields in the forms, because I assume that if all the form field names are different, there’d be no issue.)

http://tareq.wedevs.com Tareq

Thanks for these nice tute. I noticed this post many days ago and decided to read it later. Today i got the chance. This was very useful to me.

Prabhas

I have a question..

If I have two forms (identified by ‘name’ property) with similar input field names, then how to specify which form field to be populated?

Jayesh

Really nice article…
I have used this technique and solved a problem quickly, actually I was looking for such a technique to post data to another server.

Thanks dude and keep it up.

Opsix

I will like to know how i can automatically submit some data to a website form without visiting the website and then get the result in a variable
the remote website is in javascript form but i need to use php to submit some data to it and return results to a php variable on my own site
Any Help?
Thanks

admin

if the form is created with js, you can’t submit the form using cURL. however, you may do a trick. look the field names and action of the form. then make a php script that submits values to that action URL. then call the php script using your javascript (ajax call) as you can’t access the remote URL in javascript.

File registracija_1.php is on the same server (in the same folder) like the file which contains this code. But wen this file is loaded it shows me a black page, and don’t redirect me to registracija_1.php.

Thank you for your effort for writing this post. Its very informative.

Just a confusion as I am very new to cURL. I am trying to write a code in which i want to send a keyword to search box of another website and return if I get any positive result or not. Will this script of urs work? What changes do I need to make? Please help me! Thanks a lot!

I’m trying to post field data to a remote .asp page that posts to itself and then displays an answer in an input field on that same .asp page. I know the fields to use in your curl example code, and the correct url, but I don’t know how to get the resulting url page that displays (with the answer) after my remote post. Any help would be really appreciated.

http://www.bluewaterinternet.com Bob

Thanks Mace,
adding:
include_once(‘simple_html_dom.php’); // from simplehtmldom, link above in Mace’s post
$html = str_get_html($result);
gave me the page content that displays after posting the form data. I was then able to parse through that with some other simplehtmldom functions and get the info I needed.