posting POST[] variables

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
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

posting POST[] variables

Post 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. :)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

curl can do that for you
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

In this case if you want to simulate a POST, CURL is the way to go. I'm with shiznatix...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 ;)
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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?
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post 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]
Last edited by RobertPaul on Wed Dec 07, 2005 9:14 pm, edited 1 time in total.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

LOL you guys are good. Alright thannks very much. ;) :lol:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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!
Post Reply