Page 1 of 1

posting POST[] variables

Posted: Wed Dec 07, 2005 8:57 pm
by evilmonkey
Hi guys,

I need to use a PHP script to load a website with some POST-passed variables. If it were a human doing it, he'd just enter the values into the textboxes..can a script do it? Is it sent in the headers? Thanks!

Note: this isn't for any illigitimate purposes. :)

Posted: Wed Dec 07, 2005 9:00 pm
by shiznatix
curl can do that for you

Posted: Wed Dec 07, 2005 9:02 pm
by evilmonkey
shiznatix wrote:curl can do that for you
In my expirience, anything you can do with curl you can do without curl with much less headaches. ;) Is there another way?

Posted: Wed Dec 07, 2005 9:04 pm
by neophyte
In this case if you want to simulate a POST, CURL is the way to go. I'm with shiznatix...

Posted: Wed Dec 07, 2005 9:08 pm
by John Cartwright

Code: Select all

$url="http://anything";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "field1=value1&field2=value2");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec ($ch);
curl_close ($ch);
Reading the manual was easy ;)

Posted: Wed Dec 07, 2005 9:11 pm
by evilmonkey
Okay, if I want to use curl then, is it something like this?

Code: Select all

<?php
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, "http://www.example.com/");
curl_setopt($curl, CURLOPT_POSTFIELDS, ); //what goes there?

curl_exec($curl);

curl_close($curl);
?>
EDIT, okay the above answered that...now what if i want to save the HTML of the page that results? In the ablove example, would $content hold what I need?

Posted: Wed Dec 07, 2005 9:13 pm
by RobertPaul
Headache? :? You're lookin' at like, 6 lines of cURLing to do what you want to do.

Code: Select all

$ch = curl_init(); //initialize handle
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //return data from cURL execution rather than outputting to browser
curl_setopt($ch, CURLOPT_POST, 1); //enable POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); //where $postData = "value1=whatever&value2=whatever"
curl_setopt($ch, CURLOPT_URL, $yourURL); //set the page you want to access
$pageData = curl_exec($ch); //and you're on your way
See? Not so bad. :wink:


[EDIT] Looks like I'm slow... [/EDIT]

Posted: Wed Dec 07, 2005 9:14 pm
by evilmonkey
LOL you guys are good. Alright thannks very much. ;) :lol:

Posted: Wed Dec 07, 2005 9:15 pm
by John Cartwright
what goes here?
Your post values, look at the above snipplet I just posted. Also are you wanting to grab the results of the page? For instance, is your page returning a specific value your supposed to grab to determine the output?

For example, on several credit card gateways our API tells the user what values are expected back, upon a transaction.. usually in the form of

Code: Select all

returned[transid::434243,status::accepted]
OR are you simply wanted to pass the information along and leave it be?

Posted: Wed Dec 07, 2005 9:17 pm
by evilmonkey
This is still a concept to me, I don't have any proctical uses for it (yet), I just want to see if I can use a POST and grab the HTML of the resulting page. I might then look for certain strings or whatever I choose to use it for.

Posted: Wed Dec 07, 2005 9:28 pm
by evilmonkey
Hmm...I did a successful (failed due to wrong password :lol: ) devnetwork forum login using this method, seems to be working. Thanks guys!

Posted: Wed Dec 07, 2005 10:33 pm
by John Cartwright
evilmonkey wrote:Hmm...I did a successful (failed due to wrong password :lol: ) devnetwork forum login using this method, seems to be working. Thanks guys!
Congradulations :D .. cURL is definantly much less complicated than it appears to be. Its amazing what the manual and user comments can offer!