【问题标题】:laravel post data and redirect to the same post url without form submitlaravel 发布数据并重定向到相同的帖子 url 而无需表单提交
【发布时间】:2019-05-31 03:26:10
【问题描述】:

我需要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

  1. 我尝试redirect() 到带有发布数据的网址,但我得到“此路由不支持 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');

  1. 我尝试使用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;
}

任何建议将不胜感激!

【问题讨论】:

  • 您需要在使用 guzzle 发出 post 请求后手动执行重定向 (return redirect())。 payment.com 文档告诉您具体做什么?是不是需要重定向到api请求返回的部分数据的url?
  • @Jeemusu payment.com 文档告诉我通过表单提交发布付款数据
  • @Autodesk 将 $client-&gt;post(url('payment.com') 更改为 $client-&gt;post('http://payment.com')
  • @Jeemusu 重定向url和post url是一样的:payment.com
  • 谢谢@AdityaThakur,事实上这是我的打字错误......哈哈

标签: laravel guzzle


【解决方案1】:

您需要从重定向历史记录中检索最后一个位置并重定向到该位置。

查看 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);

【讨论】:

    猜你喜欢
    • 2019-04-22
    • 1970-01-01
    • 2014-02-27
    • 2019-08-31
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2017-03-14
    相关资源
    最近更新 更多