PHP重定向外部站点和POST json

时间:2013-09-18 13:24:23

标签: php json post redirect curl

我正在将注册与其他网站集成。另一个网站将我的网站在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

}

2 个答案:

答案 0 :(得分:1)

这在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");

答案 1 :(得分:0)

您可以使用PECL的http_redirect功能。

查看文档:{​​{3}}

相关问题