Sending a POST

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Odinian
Forum Newbie
Posts: 1
Joined: Sat Jun 04, 2005 4:50 pm

Sending a POST

Post by Odinian »

Is there any way using only PHP to send a post command with certain variables?

Basically, I want to set several variables and send them as POST data.

So I can define something as $postvar_username, do something like post_to("url"). Then, at that URL, I would like to be able to use $_POST['username'], and get the value from the previous page.

Thanks for all help in advance!
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

For the PHP library, see cURL

Simpletest, however, contains a scriptable browser suite that lets you do stuff like:

Code: Select all

$browser = &new SimpleBrowser();
 $browser->get('http://my-site.com/register.php');
 $browser->setField('email', 'me@here');
 $browser->setField('password', 'Secret');
 $browser->clickSubmit('Register');
Which is a lot easier to use than cURL (the standard response you'd get).
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Maybe send it in the url as a variable and use GET on the next page

hth
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

He wants to use POST, not GET. If you wanted to use POST without any external libraries, he'd end up opening up the socket himself and manually compiling the HTTP request data (very messy).
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

True, but GET will acheive what he wants ..

Just a thought for him thats was all.
Last edited by Jim_Bo on Sat Jun 04, 2005 9:10 pm, edited 2 times in total.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Then, at that URL, I would like to be able to use $_POST['username'], and get the value from the previous page.
Hmm... I must have missed that.
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post by Drachlen »

Code: Select all

<BODY>

<FORM METHOD="POST" ACTION="postto.php" ID="postform">
	
	<INPUT TYPE="HIDDEN" NAME="username" VALUE="Billybob">

</FORM>

</BODY>

<SCRIPT>

	document.getElementById("postform").submit();

</SCRIPT>
This might be what you're looking for. One issue though, If you intend to pass a variable named "submit" it's going to get confused and break. So if whatever you're posting to requires that variable to be passed, It will fail and require an alternative:

Code: Select all

$variables  = "username=Billybob";
$variables .= "&submit=Submit";

$socket = fsockopen("website.com",80);
fputs($socket,"POST /path/file.php HTTP/1.1\r\n");
fputs($socket,"Referer: Intarweb\r\n");
fputs($socket,"Host: Host\r\n");
fputs($socket,"Accept: text/html\r\n");
fputs($socket,"User-Agent: Intarweb Browser/1.0\r\n");
fputs($socket,"Content-type: application/x-www-form-urlencoded\r\n");
fputs($socket,"Content-length: ".strlen($variables)."\r\n");
fputs($socket,"Connection: Close\r\n");
fputs($socket,"\r\n");
fputs($socket,$variables."\r\n");



while(!feof($socket)){
    $a = fgets($socket,256);
    if ($a){
        echo $a."<br>";
    }
}
Post Reply