Redirect API in PHP with POST data

Questions:

I created a new API that receives XML data in POST and responds in XML. The link is

localhost/api/rate.php

Now, users were already accessing the old version of this API using the following link,

localhost/rate.php

I need to redirect to the new link when the user hits the old link and displays the response from the new API to the user.

If I user header(location) the POST data does not get sent to the new API, so I’m out of options.

How to&Answers:

you could do like this:

your rate.php:

<?php

function redirect($url) {
  $html = "<html><body><form id='form' action='$url' method='post'>";
  foreach ($_POST as $key => $value) {
    $html .= "<input type='hidden' name='$key' value='$value'>";
  }
  $html .= "</form><script>document.getElementById('form').submit();</script>";
  $html .= "</body></html>";
  print($html);
}

redirect('api/rate.php');

or safer:

.htaccess file with:

RewriteEngine On
RewriteRule ^rate.php(.*)$ /api/rate.php$1 [R=302]

Leave a Reply

Your email address will not be published. Required fields are marked *