Jump to content

Send POST request on page load?


maratonic

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.