0

I have an application on Yii2.

I have an external vendor i want to redirect. For example, i am encrypting the current user info, and want to send that to the external vendor, so the user information is automatically populated on his website.

Everything works fine if i do that on the front end side using JS.

The problem i have is only my app server is whitelisted to the vendor. So i need to make the redirection happen on the backend.

I tried several method, and even with Guzzle HTTP, and it doesn't redirect.

TO be clear, i am not looking for a simple redirect, it is a form post on external URL.

I went though this post, but the problem remain the same... Yii2 how to send form request to external url

Does anybody faced this issue?

1 Answers1

0

If I understand you correctly, you want to POST data received from your frontend to the server of a third party company (let's call them ACME). Your webserver is whitelisted by ACME but your clients are not (which is good). You want to show a page on your server and not a page on the ACME server after the POST, right?

You have to send the data with a separate request from your server to the ACME server. You cannot create a HTML <form> and put the ACME server in the action parameter of it, because the request would be sent from the client and not your backend, failing at the whitelist. After you validate your client input, you can send it with guzzle to ACME.

With the status code you can decide if your request was successful or not. You can read more about guzzle here

The code sample below shows how you could POST the $payload (a fictional blog post if you will) as JSON to the ACME server and output a line with the request result.

$payload = [
    'username' => 'john.doe',
    'postContent' => 'Hello world',
];

$client = new GuzzleHttp\Client();
$response = $client->post('https://acme.example.org/endpoint',['http_errors' => false, 'json' => $payload]);

if($response->getStatusCode() === 200){
    echo "Request OK";
}else{
    echo "Request failed with status ".$response->getStatusCode();
}
DBX12
  • 2,041
  • 1
  • 15
  • 25