简体   繁体   中英

Send post request from one site to another

I was wondering, the firefox addon, (hackbar) has the option to send a post request to the given website. Now the question is how do I send a post request in a php script to another site? In this case the only post data will be: nick=bob . The site I want to send the post request to is: www.example.com . The php script will be hosted on www.host.com . Thanks to anyone who can provide me with a straight forward php script to do this that can have such data easily replaceable with something else that I can suit to my own preference.

Do you mean that you want the end user of your website to send a POST request from their browser, or that you want your server-side code to send a POST request?

If the former, you can simply use that other site as the action in a form :

<form method="post" action="http://www.example.com/somepage.php">

If the latter, you can use curl to send a POST request.

Edit: Based on comments, it looks like what you're trying to achieve is to have a client-initiated form POST, but to initiate it from code without user interaction. In this case you'd still have a form in the page. Though it doesn't need to be displayed to the user. Perhaps it could contain only hidden fields, for example. Something like this:

<form id="someHiddenForm" method="post" action="http://www.example.com/somepage.php">
    <input type="hidden" name="field1" value="value1" />
    <input type="hidden" name="field2" value="value2" />
</form>

Then in JavaScript you can invoke the submit action on that form:

document.getElementById('someHiddenForm').submit();

Just be careful with something like this. Automatically posting a form to another site without user interaction is very close to a couple of different attempts at security exploits. And at the very least may be an undesirable action for some users. Just be aware of that.

If you want to get an outside page for your php script I think you should use cURL for that. It will give you whatever that page gives you.

But if you just need some data from that site you could use ajax to do it client side too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM