views:

69

answers:

5

Using PHP can you create a pseudo form submit without ever generating a form? Just initialize and declare variables and them pass them to another page via the POST or GET methods?

A: 

Definitely. You can do this using cURL, this link should explain the basics: http://curl.haxx.se/docs/httpscripting.html - but there are hundreds of cURL tutorials on the internet for doing things like this.

Once you get the hang of cURL, it's quite easy to understand and use.

Here is a code example:

<?
define('POSTURL', 'http://www.test.com/search.php');
 define('POSTVARS', 'listID=29&request=suba&SubscribeSubmit=Subscribe&EmailAddress=');// POST VARIABLES TO BE SENT

 $ch = curl_init(POSTURL);
 curl_setopt($ch, CURLOPT_POST      ,1);
 curl_setopt($ch, CURLOPT_POSTFIELDS    ,POSTVARS);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
 curl_setopt($ch, CURLOPT_HEADER      ,0);  // DO NOT RETURN HTTP HEADERS
 curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL
 $data = curl_exec($ch);

?>

Something like that should work.

Dwayne
It was my understanding cURL was not meant to used to pass data within a server, but server to server instead. Thanks for the tutorial.
Brook Julias
There's no reason it couldn't be used to pass data within a server.
ceejayoz
You're not really going to be able to POST data anywhere without using cURL or some other library, I'm not aware of any others that come close to cURL.
Dwayne
@Dwayne You can use PHP's http stream wrapper. But everyone seems to love cURL.
Artefacto
@Artefacto - I had noticed the love of CURL. Supposedly it's the cleaner method do accomplish this task, but from some of the code I have it doesn't look that way.
Brook Julias
+4  A: 

What you're looking for is cURL, a library for creating such requests.

Joseph Mastey
I thought cURL was not meant to used to pass data within a sever, but should be used for server to server submissions.
Brook Julias
As long as you hit the script through the localhost, it should execute as normal.
Oren
What you want to do is a server to server transfer, it just happens that its the same server. If you're trying to pass data within internally, just call that function/code from whatever PHP page you want. No need to go through the HTTP protocol to talk to your own server.
Baddie
Calling the function or code would not be an adequate solution. The variable being passed between pages will be used in the displaying on the other page. It will determine how some of the HTML is built.
Brook Julias
A: 

Theoretically, you could generate internal requests in Apache for this form submission; in practice, you'll have to use PHP's http stream wrapper or curl, like the others suggested.

Artefacto
A: 

You can try doing something like this

$_POST = array(
                        'lname'=>urlencode($last_name),
                        'fname'=>urlencode($first_name),
                        'title'=>urlencode($title),
                        'company'=>urlencode($institution),
                        'age'=>urlencode($age),
                        'email'=>urlencode($email),
                        'phone'=>urlencode($phone)
                );

$_GET = array(); // no get
$_SERVER['REQUEST_METHOD'] = "POST";
$_SERVER['REQUEST_URI'] = '/anotherpage.php';
// set REQUEST_PATH, REQUEST_PATHINFO, REQUEST_SCRIPTNAME etc
// now it will be like this anotherpage.php was requested with a form
include("anotherpage.php");

If you want to avoid cURL but I don't recommend it because it gets messy fast.

Chris T
I like the example code you provided, but I can see where things would get messy quickly.
Brook Julias
A: 

I am not sure if you can set POST options, but as long as GET is acceptable you could do this very easily (i'm not expert enough to rattle this off without testing and promise no bugs, so you'll have to test):

$url = "http://foo.com?"
while(list($key, $value) = each($_POST)
 $url .= "$key" . "=" . $value . "&";
header(Location: $url);

This will turn your POST into a GET.

Chris Sobolewski
I thought I could do something like this. Thanks for the example code.
Brook Julias