acdx Posted September 2, 2006 Share Posted September 2, 2006 HeyHow 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 Quote Link to comment Share on other sites More sharing options...
w32 Posted September 2, 2006 Share Posted September 2, 2006 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... :( Quote Link to comment Share on other sites More sharing options...
acdx Posted September 2, 2006 Author Share Posted September 2, 2006 I'm not sure how to do that.. Quote Link to comment Share on other sites More sharing options...
w32 Posted September 2, 2006 Share Posted September 2, 2006 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 Link to comment Share on other sites More sharing options...
acdx Posted September 2, 2006 Author Share Posted September 2, 2006 [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? Quote Link to comment Share on other sites More sharing options...
w32 Posted September 2, 2006 Share Posted September 2, 2006 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 Quote Link to comment Share on other sites More sharing options...
acdx Posted September 2, 2006 Author Share Posted September 2, 2006 Just realized this should be possible with fsockopen() etc, right? Quote Link to comment Share on other sites More sharing options...
w32 Posted September 2, 2006 Share Posted September 2, 2006 yip :)you'll still need a form if using POST, lol ;)actually no...wouldnt work in the exact same way...kinda troublesome u know... Quote Link to comment Share on other sites More sharing options...
acdx Posted September 2, 2006 Author Share Posted September 2, 2006 [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.. Quote Link to comment Share on other sites More sharing options...
w32 Posted September 2, 2006 Share Posted September 2, 2006 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 Quote Link to comment Share on other sites More sharing options...
acdx Posted September 2, 2006 Author Share Posted September 2, 2006 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.0Content-Type: application/x-www-form-urlencodedContent-Length: 14a=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]<?phpecho($_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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 2, 2006 Share Posted September 2, 2006 You need to use either fsockopen() or CURL functions.CURL: http://www.php.net/curlfsockopen: http://www.php.net/fsockopenThere is an user contribution on the fsockopen page which may help you.Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.