DecoK
DecoK

Reputation: 145

PHP redirecting external site and POST json

I am integrating a registration with another website. The other website posts my site some registration data in json, to a registration page on my site and prepopulating the fields. I take the user through the registration, then on success redirect back to the original site. This redirect is supposed to post some info back, in json format, the user ID etc. I'm not sure how to go about redirecting back and posting this data? I'd rather not use a JS form submit.

Any help appreciated.

//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

}

Upvotes: 1

Views: 2728

Answers (2)

Ander2
Ander2

Reputation: 5658

You can use PECL's http_redirect function.

Have a look at the documentation: https://php.uz/manual/en/function.http-redirect.php

Upvotes: 1

DanO
DanO

Reputation: 10270

This isn't possible within PHP. You could use CURL to post the JSON to the other page, then do a redirect to that page, but you cannot redirect AND post at the same time.

For the CURL method, here is some sample code:

$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");

Upvotes: 1

Related Questions