PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

HOW TO POST DATA WITHOUT FORMS IN PHP

Sometimes you will want to post information to another site but you want to do this dynamically, on the fly without using a physical form on a site where information has to be entered.

This tutorial explains how you can do this quite easily using curl. PHP on your server will need to be compiled with curl support enabled. If you want to check quickly if it is, do the following:

Create a php file with the following code:

<?
phpinfo();
?>

Now run this file and you will see details of your PHP installation. Look for the curl sub-heading and see whether cURL support is enabled. If it is, then you set. If not, then you may want to contact your host and ask them to install it for you. It is relatively easy and free for them to do it for you.

Now that you have the curl library installed, let's move on to the relatively simple few lines of code:

<?

$URL="www.thesite.com/somepage.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://$URL");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=John&email=john@site.com");
curl_exec ($ch);
curl_close ($ch);

?>

$URL="www.thesite.com/somepage.php";

This is the destination url where you want to post to. Set it to your requirements.

curl_setopt($ch, CURLOPT_POSTFIELDS, "name=John&email=john@site.com");

These are the name value pairs that you want to post. The values can also be dynamic variables from this script eg:

$name="John";
$email="john@site.com";
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=$name&email=$email");

You edit or add name/value pairs as you require.

The data can be retrieved at the destination site using the usual POST method ie: $name=$_POST['name'] and $email=$_POST['email'].

If $_POST doesn't work, try $_REQUEST instead.

Home | Privacy Policy | Terms Of Use | Contact Us