0

I have an API link like :

www.mydomain.com/api.php?key=test&action=say&what=hi

And I would like to access it like :

www.mydomain.com/api.php/test/say/hi

Is it possible ? I didn't found.

1 Answers1

1

If you are getting API url modification using php then you can use this below function

<?php
//Suppose we have URL in string format
//$apiLink = 'www.mydomain.com/api.php?key=test&action=say&what=hi';

//if we doesn't know, or it will be dynamic all times getting from browser
$apiLink = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$sendNewUrl = makeNewURL($apiLink);
header("location: ".$sendNewUrl);

function makeNewURL($apiLink){
    $totalParams = "";
    $apiData = explode("?",$apiLink);
    $apiParameters = explode("&",$apiData[1]);
    if(count($apiParameters)>0){
        foreach($apiParameters as $params){
            if(strpos($params, '=') !== false){
                $expected = explode("=",$params);
                $totalParams .= $expected[1];
                if($totalParams!=""){
                    $totalParams .= "/";
                }
            }
        }
    }
    return $apiData[0]."/".$totalParams;
}
Ajeet Kumar
  • 805
  • 1
  • 7
  • 26