Page 1 of 1

Is there a way to use POST using just code and no forms?

Posted: Sun May 18, 2008 3:13 am
by mirra1515
I am trying to send data with POST variables using mostly php code. I know that a user could click on a form and have it transmit POST variables to a script; however, I am looking to to do just the opposite and transmit from a script post data.

For example:

Code: Select all

 
<?php
 
//A string too long for GET
$tooLongForGET = "Ipsum epsum lorem indige...you get the idea..."
 
//Another string too long for GET
$tooLongForGET2 = "Ipsum epsum lorem indige..."
 
$URL="http://localhost/test/form.php";
header ("Location: $URL");
 
?>
The "header ("Location: $URL")" command has given me trouble since I can't echo data before I issue that command or else I get an error; therefore, I can't use some of the usual options such as forms and so forth.

I have had to store the variable's contents to a mysql database in order to pass on the data to the form; however, I would MUCH rather just send the data using POST variables or some other similar method. How would I send those two variable's data to the page "form.php" automatically without waiting for a user to click any buttons or anything? Thanks in advance!

Re: Is there a way to use POST using just code and no forms?

Posted: Sun May 18, 2008 10:23 am
by sim-and-sim
ha, my second post here and they're both on the same place...

if you want to post from one file to another without using a form you use a thing called cURL, this thing scared the crap out of me the first time I used it, but once you get your head around it its quite simple. I'll try to keep this sweet and short.

Code: Select all

 
 
$ch = curl_init();  //This here just starts the cURL, kinda like session_start();
 
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "$URL"); //Set the file you want to post to here, you called it $url
 
 
// Do a POST
$data = array("var1" => "value1", "var2" => "value2"); //These are your post fields, the first is the post name just like the name="var1" in a HTML form, 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //This returns it as a var, it'll be explained at the end
curl_setopt($ch, CURLOPT_POST, true); //Tells the cURL you want to post values
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //Puts the POST Values into the post
 
// grab URL, and print
$output = curl_exec($ch); //This is your file returned, this is what the 3rd option up from this does, without that option this will just be printed out at this point... 
 
curl_close($ch); //This closes the cURL... go figure....
 
I hope that helps you...

Re: Is there a way to use POST using just code and no forms?

Posted: Sun May 18, 2008 11:30 am
by s.dot
Or just send headers [this is from my paypal ipn script]

Code: Select all

//set up POST headers
$headers = 'POST /cgi-bin/webscr HTTP/1.0' . "\r\n";
$headers .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
$headers .= 'Content-Length: ' . strlen($request) . "\r\n\r\n";
Then open the connection to the page with fsockopen() and write the headers and the request with fwrite()

Code: Select all

//open a socket connection to PayPal
if (!$fileStream = @fsockopen('www.paypal.com', 80, $errno, $errstr, 30))
{
    //there was an error connecting, error
} else
{
    //we have a connection to PayPal
    //write the data to the file stream pointer
    fwrite($fileStream, $headers . $request);
}