PHP 重定向外部站点和 POST json


PHP redirecting external site and POST json

我正在将注册与另一个网站集成。另一个网站将我的网站的一些注册数据以 json 格式发布到我网站上的注册页面并预填充字段。我带领用户完成注册,然后在成功后重定向回原始站点。此重定向应该以 json 格式、用户 ID 等发布一些信息。我不确定如何重定向回并发布此数据?我宁愿不使用JS表单提交。

任何帮助表示赞赏。

//After I have registered the user on my site and it is successful  
if($success){
$postJsonArray = json_encode(array('success' => TRUE, 'userID' = 10));
$postUrl = 'http://thirdpartysite.php';
//somehow redirect and post this $postJsonArray to the URL
}

在PHP中是不可能的。您可以使用 CURL 将 JSON 发布到另一个页面,然后重定向到该页面,但不能同时重定向和发布。

对于 CURL 方法,下面是一些示例代码:

$ch = curl_init();
$json = json_encode($data);
curl_setopt($ch,CURLOPT_URL, $curl_url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$json);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//execute post
$result = curl_exec($ch);
curl_close($ch);
header("Location: http://example.com/redirect");
您可以使用

PECL的http_redirect函数。

查看文档:https://php.uz/manual/en/function.http-redirect.php