Php Yii2如何将表单请求发送到外部url
如何将重定向post请求发送到外部url?控制器中的我的代码:Php Yii2如何将表单请求发送到外部url,php,post,yii2,Php,Post,Yii2,如何将重定向post请求发送到外部url?控制器中的我的代码: if ($model->load(Yii::$app->request->post()) && $model->validate()) { // send post request to external link } 你可以很容易地用它。我认为下面的代码正是你想要的 if ($model->load(Yii::$app->request->po
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// send post request to external link
}
你可以很容易地用它。我认为下面的代码正是你想要的
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$response = $client->request('POST', 'http://httpbin.org/post', [
'form_params' => [
'field_name' => 'abc',
'other_field' => '123',
'nested_field' => [
'nested' => 'hello'
]
]
]);
}
您可以使用HTTP 301重定向请求
Yii::$app->response->redirect('url', 301);
Yii::$app->end();
或者使用任何php http客户端,如Guzzle(请参阅)
没有其他选项。您可以使用CURL将数据发送到外部服务器 您可以使用以下代码将post请求发送到URL
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$url = 'http://www.example-redirect.com';
$host = "http://www.example.com";
$postData = Yii::$app->request->post();
$ch = curl_init($host);
$data = http_build_query($postData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Redirect from here
return $this->redirect(Url::to($url));
}
您需要使用状态代码来指定应该使用相同的POST数据执行的重定向
$this->redirect('https://example.com', 307);
HTTP 307临时重定向重定向状态响应代码表示请求的资源已临时移动到位置
标题给定的URL
原始请求的方法和主体被重用以执行重定向请求
答案是:你不能。您可以在会话中保存变量或构建html表单,并通过JavaScript自动提交。为什么要这样做?可能重复@AntonRybalko Hi,因为我想重定向到托管页面(这是一个外部链接)它需要通过http post请求表单重定向发送参数。我不能在这个表单上使用curl,因为它需要重定向。301重定向将丢失post数据-您肯定不想这样做。您可以将post数据作为get参数发送。或者通过php http客户端发送post请求。您好,谢谢,但它需要重定向到托管页面。要重定向,您可以使用Url帮助器<代码>返回$this->重定向(Url::to('https://www.example.com'));你好,罗布,谢谢你的邀请。我可以发送我的post请求并被重定向,但是服务器没有确认我的请求,但是当我使用常规html表单(不是在yii2中)时,它会接受我的请求。有什么想法吗?干杯