问题
I'm trying to take some variables in php and redirect the user to a third party site with the variables sent as post vars. I know this would probably be easier with cookies but the third party is already set up to handle posts. Does anyone have any suggestions on how to do this?
回答1:
There are a couple techniques you could use. One is iterate through the POST vars and add to a form and resubmit the form perhaps using Javascript onLoad.
<html>
<body onload="document.forms[0].submit()">
<form action="new-location.php" method="post">
<?php foreach( $_POST as $key => $val ): ?>
<input type="hidden" name="<?php echo htmlSpecialChars( $key, ENT_COMPAT, 'UTF-8' ) ?>" value="<?php echo htmlSpecialChars( $val, ENT_COMPAT, 'UTF-8' ) ?>">
<?php endforeach; ?>
</form>
</body>
</html>
Another option would be to use PHP and cURL and send the data to the remote site:
url_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
http://www.php.net/manual/en/function.curl-setopt.php
回答2:
In a PHP page, open a connection to the third-party website using cURL and use this function :
curl_setopt($Curl, CURLOPT_POST, 1);
curl_setopt($Curl, CURLOPT_POSTFIELDS, 'field1=allo&field2=fsdfsd');
The third-party will receive the request with also a real POST request. You'll be able also to read the answer from the third-party website.
回答3:
A post needs to be sent from the users browser. A common way to do this is to use an intermediate page which would include a form with hidden input elements containing the variables you want to post, then using Javascript to automatically submit the form on load.
回答4:
Some googling suggests that other than horrible kludges with forms, cURL is the way to go. I'm no expert, but this link http://www.electrictoolbox.com/php-curl-form-post/ and the php cURL reference should be helpful.
回答5:
Not enough information, but maybe you can send it using form with help of javascript (jQuery)
<script>
$(function(){
$('#form').submit();
});
</script>
<form action="external.url/page.php" action="post" id="form">
<input type="hidden" name="variable" value="value" />
</form>
来源:https://stackoverflow.com/questions/11637854/php-post-vars-to-third-party