Jump to content

Archived

This topic is now archived and is closed to further replies.

acdx

Capturing POST response

Recommended Posts

Hey

How can I send a POST request to a server and capture the response (HTML page) in a string? Kinda like you can capture GET output with eg. file_get_contents().

Thanks

Share this post


Link to post
Share on other sites
I thought that function worked with Post as well....try it...with application/x-www-form-urlencoded on your enctype...if not I wouldnt know... :(

Share this post


Link to post
Share on other sites
Do it exactly as you'd have done it using the GET method, just use a form like this one:
<form action="your-script.php" method="post" enctype="application/x-www-form-urlencoded">

</form>

Share this post


Link to post
Share on other sites
[quote author=w32 link=topic=106634.msg426556#msg426556 date=1157211655]
Do it exactly as you'd have done it using the GET method, just use a form like this one:
<form action="your-script.php" method="post" enctype="application/x-www-form-urlencoded">

</form>
[/quote]

That's not what I want to do..

Let me explain.. I want to send a POST request that includes form data using php to a remote server/page and capture the response into a string.

This were easy if the script accepted GET form data (form data in the URL)

[code]<?php
$output = file_get_contents("http://a.com/script.php?a=this&b=that");
?>[/code]

How can I do this with POST?

Share this post


Link to post
Share on other sites
mmm as I told you, personally I dont' know any other way to POST information to the server without the use of a form...  :D

Share this post


Link to post
Share on other sites
yip :)

you'll still need a form if using POST, lol ;)


actually no...wouldnt work in the exact same way...

kinda troublesome u know...

Share this post


Link to post
Share on other sites
[quote author=w32 link=topic=106634.msg426595#msg426595 date=1157213849]
yip :)

you'll still need a form if using POST, lol ;)
[/quote]
In my scenario, it's not the user that POSTs the data and looks at the response, it's my PHP script that does..

Share this post


Link to post
Share on other sites
ooohh should've said that...well show me how you're using post in your script, and I'll try to use file_get_content() (should work just as GET)

because fsockopen to my knowledge wouldnt help ya

Share this post


Link to post
Share on other sites
I believe I can't do it with GET as the remote script only accepts POST.

I got things working with fsockopen() however. I tested this on my own server.

[code]<?php
$c = fsockopen([my hostname], 80);
$rq =
"POST /ptest.php HTTP/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 14

a=blah&b=blah2

";
fwrite($c, $rq);
while(!feof($c))
{
echo(htmlentities(fgets($c))."<br />");
}
?>[/code]

The receiving end in this test simply prints the variables:

[code]<?php
echo($_POST["a"]." and ".$_POST["b"]);
?>[/code]

As said, this is only a test where both files reside on my own server and the output is simply printed to the user.

Share this post


Link to post
Share on other sites
You need to use either fsockopen() or CURL functions.
CURL: http://www.php.net/curl
fsockopen: http://www.php.net/fsockopen

There is an user contribution on the fsockopen page which may help you.

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.