我需要post
数据到付款服务(payment.com),redirect
到Payment.com(填写信用卡号等)
传统方式是这样的:
<form id="form" action="payment.com" method="POST">
</form>
<script type="text/javascript">
document.getElementById('form').submit();
</script>
但是我想通过
controller
而不是通过表单提交,所以没有 数据可以更改。
这是我的控制器方法,我尝试了两种方法:redirect()
和Guzzle
redirect()
到URL,但是我得到了“此路线不支持GET方法。受支持的方法:POST。” public function postToPaymentServer(Request $request)
{
$amount=$request['amount'];
$payment=[
'amount'=>$amount,
'auth-id'=>config('auth-id')
];
return redirect(url('api/payment/server'))->with(compact('payment'));
}
ps。在这里,我做了一条本地路线来模拟routes / api.php
中的payment.com。Route::post('payment/server','PaymentController@server');
Guzzle
,但它不会重定向到帖子网址。public function postToPaymentServer(Request $request)
{
$amount=$request['amount'];
$payment=[
'amount'=>$amount,
'auth-id'=>config('auth-id')
];
$client = new Client();
$response = $client->post('payment.com',[
'body'=>[
'payment'=>$payment,
'allow_redirects' => true
],
]);
return $response;
}
任何建议将不胜感激!
答案 0 :(得分:0)
您需要从重定向历史记录中检索最后一个位置,然后重定向到该位置。
查看 track_redirects 选项:
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
$httpClient = new Client();
$response = $httpClient->post(
'http://payment.com',
[
RequestOptions::ALLOW_REDIRECTS => [
'max' => 5,
'track_redirects' => true,
],
RequestOptions::FORM_PARAMS => [
'payment' => $payment,
],
]
);
$lastLocation = end($response->getHeaders()['X-Guzzle-Redirect-History']);
return redirect($lastLocation);