maratonic Posted June 4, 2010 Share Posted June 4, 2010 Hi all, What I want to do is this: when the user fetches a page from my server, a POST request should be sent to another server than the one my site is on. Like: call http://theotherserver.com/somescript.php with variable1=3 and variable2=fred. The data that is sent does not require user interaction, I send it with the page from my server. I want the data to be sent from the client, not from my server. I'm not familliar with how HTTP requests are constructed and therefore my "invesigations" have lead to two thoughts that might be completely off target. 1. Include something like this: <img src="http://theotherserver.com/somescript.php?variable1=3&variable2=fred" height="0" width="0" /> but I suspect that this won't produce a valid POST request (but rather a GET request?) 2. Using curl: <?php $url = 'http://theotherserver.com/somescript.php'; $ch = curl_init(); curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POST,2); curl_setopt($ch,CURLOPT_POSTFIELDS,'variable1=3&variable2=fred'); $result = curl_exec($ch); curl_close($ch); ?> but I suspect this will send the request from my server, not from the client. SO, do you have any ideas on how to solve it? (If it can even be done?) Thankful for any response. /Fredrik Link to comment https://forums.phpfreaks.com/topic/203837-send-post-request-on-page-load/ Share on other sites More sharing options...
F1Fan Posted June 4, 2010 Share Posted June 4, 2010 You're right on both accounts. Since PHP is a server-side language, to do it only in PHP would mean doing it from your server. Therefore, you need client-side scripting, or JavaScript. Try something like this: <body onload="document.forms[0].submit();"> <form method="post" target="iframePost" action="http://theotherserver.com/somescript.php"> <input type="hidden" name="variable1" value="3"> <input type="hidden" name="variable2" value="fred"> <iframe style="height:0px;width:0px;" name="iframePost" src=""></iframe> </form> </body> Link to comment https://forums.phpfreaks.com/topic/203837-send-post-request-on-page-load/#findComment-1067712 Share on other sites More sharing options...
maratonic Posted June 12, 2010 Author Share Posted June 12, 2010 Thanks! This seems to work fine! One question though: does using an iFrame make people's browsers block the call? I think there are security settings that do that but I don't know what the defaults are. /Fredrik Link to comment https://forums.phpfreaks.com/topic/203837-send-post-request-on-page-load/#findComment-1071077 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.