/    Sign up×
Community /Pin to ProfileBookmark

How to write a PHP script to fill out a remote html form?

How would I go about writing a php script that fills out a specific form with pre-specified information?

The script needs to run from a server other than the one with the form on it. I asked on irc and they said something about using sockets and http headers. Is there a way to write this that isn’t as complicated?
If there isn’t, do you know of a tutorial that explains php sockets and headers?
Or at least any source code I could study?

Thanks in advance.

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@X-rayDec 27.2005 — uhm, I'ld do it this way... make a frameset with in one frame the form and in another page your script...

yet this would have to happen in Javascript... you'ld just have to use:

[CODE]parent.frameName.formName.fieldName.value = 'value';[/CODE]
Copy linkTweet thisAlerts:
@gopher292authorDec 27.2005 — Thanks for the answer, but that's not really what I need:

The form (and perl form processor) are on the first server.

The php script I'm trying to write is on another server. (One I have access to)

The php script on my server needs to fill out the form on the first server and submit it to the perl form processor.

That should be a little clearer, my first message is kind of hard to understand. :p
Copy linkTweet thisAlerts:
@X-rayDec 27.2005 — no it's just the same, and it won't work if you don't use JS...

I'm pretty much sure of that...

the only difficulty is to obtain the posted form elements on the second server

but if you own both the servers then i suspect that you just want to get into trouble by trying to do such things.

UNLESS: if you use a frameset (as i suggested above)

and as a source you give "page.php?values=predefined"

than if you should change the php page "page.php" on server 1

when values == "predefined" or just something so you know that this is the case...

than you use another output, with all the <input type="*****" value="">

that you'ld wish to define
Copy linkTweet thisAlerts:
@acorbelliDec 27.2005 — So are you trying to create an automated form fillout script?
Copy linkTweet thisAlerts:
@felgallDec 27.2005 — it won't work if you don't use JS...[/QUOTE]

It definitely wont work with Javascript because Javascript cannot work between pages served from differendt servers.

Take a look at socket processing to read the content of the page from the first server into the page on the secod from where it can then be modified before sending to the browser.
Copy linkTweet thisAlerts:
@gopher292authorDec 27.2005 — X-ray: I get what you're saying now, that might work. ?

acorbelli: Yes, that is exactly what I'm trying to do.
Copy linkTweet thisAlerts:
@X-rayDec 27.2005 — It definitely wont work with Javascript because Javascript cannot work between pages served from differendt servers.
[/QUOTE]

uhm, if you can do it with framesets and you know the form names of the page on an external server then I wouldn't know why it wouldn't work... ?
Copy linkTweet thisAlerts:
@gopher292authorDec 27.2005 — The first server I don't have access to as a normal or root user.

And I know the html form's structure (I just view source from a browser)
Copy linkTweet thisAlerts:
@X-rayDec 27.2005 — damn, you might have a problem there...

because in my knowledge you can't return the form submitted values to a page that's not on your server...

that would be a catastrophy if made possible in fact, you would be able to retrieve passwords from users of any website... as long as it was opened from your pages


if there is a way, try to retrieve the page code (instantly with php)

replace the form field with the required values and replace the action="page.php" with something on your own server, then output the code...
Copy linkTweet thisAlerts:
@gopher292authorDec 27.2005 — Why wouldn't: <form method="post" action="http://firstserver.com/formhandler.pl">

<input name="argument1" size="30" type="text" value="data1">

</form>[/quote]
work?

If it would, I could do something like that.
Copy linkTweet thisAlerts:
@X-rayDec 27.2005 — because normally the http://firstserver.com/formhandler.pl should become

http://secondserver.com/formhandler.pl if you want to retrieve the data

which was submitted...

but to know what you're up to I need to have more background...
Copy linkTweet thisAlerts:
@gopher292authorDec 27.2005 — Alright, it worked! ?

Next question:

Can I modify this somehow to submit the same form to lots of identical form handlers on a bunch of different servers?

And thanks for everybody's help already! ?


Edit: X-ray, here's an example html form: [url=http://coolissues.com/cgi/links/links.html]http://coolissues.com/cgi/links/links.html[/url]

Edit Again: And my form: <form method="post" action="http://coolissues.com/cgi-bin/links/links.pl">

<input name="title" size="30" type="text" value="Title of the link">

<input type="text" name="url" size="55" value="http://www.mysite.com">

<select name="section">

<option selected>Miscellaneous</select>

<input type="submit" value="Add">

</form>[/quote]
Copy linkTweet thisAlerts:
@acorbelliDec 27.2005 — If you know the layout of the form you could theoretically make a mock up version of it on your server and use php to fill it out and then submit it to the server you need it to get to.
Copy linkTweet thisAlerts:
@ScleppelDec 27.2005 — This is a function copied from [URL=http://uk.php.net/manual/en/function.fsockopen.php#49938]a comment[/URL] from the [URL=http://uk.php.net/fsockopen]fsockopen[/URL] page on [URL=http://www.php.net/]php.net[/URL]:
[code=php]<?php
# $host includes host and path and filename
# ex: "myserver.com/this/is/path/to/file.php"
# $query is the POST query data
# ex: "a=thisstring&number=46&string=thatstring"
# $others is any extra headers you want to send
# ex: "Accept-Encoding: compress, gziprn"
function post($host,$query,$others=''){
$path=explode('/',$host);
$host=$path[0];
unset($path[0]);
$path='/'.(implode('/',$path));
$post="POST $path HTTP/1.1rnHost: $hostrnContent-type: application/x-www-form-urlencodedrn${others}User-Agent: Mozilla 4.0rnContent-length: ".strlen($query)."rnConnection: closernrn$query";
$h=fsockopen($host,80);
fwrite($h,$post);
for($a=0,$r='';!$a;){
$b=fread($h,8192);
$r.=$b;
$a=(($b=='')?1:0);
}
fclose($h);
return $r;
}

// an example of usage:
header('Content-Type: text/plain');
echo post('example.com/post-dump.php','page=1&type=3&u=p&j=90&y=?');

?>[/code]

Hope this helps.
Copy linkTweet thisAlerts:
@gopher292authorDec 27.2005 — That's just what I needed, Scleppel! ?

I've searched for hours trying to find just that, thanks a bundle. ?


Thank you again to all that posted to help me out!
×

Success!

Help @gopher292 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...