0

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.

Ali Hassan
  • 75
  • 7
  • Why don't you just make them both point to the same function? Also have you considered using something like Laravel to make your API? – iJamesPHP2 Feb 19 '20 at 00:11

1 Answers1

0

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]
db1975
  • 775
  • 3
  • 9