-5

I am wondering how I can grab all the values from the URL after .php?

Example: http://localhost/api/test

API = api.php, just .htaccess.

And I am wondering how I can grab only "test" from the URL, or al the values if more, /test/test1/test2

I've tried $_SERVER['PATH_INFO'] but it doesn't work for me, error output saying that $_SERVER['PATH_INFO'] is undefined. Is there any workaround?

Jan Sršeň
  • 1,045
  • 3
  • 23
  • 46
  • 1
    $_GET if it's set this way ... $_SERVER['REQUEST_URI'] if not – treyBake Sep 18 '18 at 08:08
  • you can refer [here](https://stackoverflow.com/questions/11480763/how-to-get-parameters-from-a-url-string) – heart hacker Sep 18 '18 at 08:10
  • But i am not doing api.php?a=test1&b=test2 SO $_GET is useless. Im trying to get the path after http://localhost/api/ – Mike Runac Sep 18 '18 at 08:11
  • Then your best chance is to get `$url = $_SERVER['REQUEST_URI'];` and then `$url = epxlode(...` then use `$url[4]` etc.. or you could replace the 'http://localhost/api/' with '' first. – Thomas J. Sep 18 '18 at 08:13
  • What server are you using? Internally your server likely _rewrites_ `/api/test` to something like `api.php?q=test` – brombeer Sep 18 '18 at 08:13
  • you need to get url using $_SERVER['REQUEST_URI'] after that store into a variable and seperate using substring like this $str = "http://localhost/api/test" echo substr($str, strrpos($str, "/") + 1); it will print test – heart hacker Sep 18 '18 at 08:16
  • 1
    @MikeRunac if you have `?` in url ... $_GET shouldn't be useless ... – treyBake Sep 18 '18 at 08:16
  • Looks like *"friendly URLs"* for a RESTful API : https://stackoverflow.com/questions/812571/how-to-create-friendly-url-in-php – CD001 Sep 18 '18 at 08:38

3 Answers3

0

Try this

Using parse_url() you can retrieve url components

$url = "http://localhost/api/test";
$myurl = parse_url($url);
echo  $myurl['path'];
Chirag
  • 363
  • 2
  • 12
0

Simple steps for you

Step One: write in .htaccess

RewriteEngine on
RewriteRule  ^(.+)/?$ test.php?url=$1

Step Two: test.php file with following code

<?php

   $url = $_GET['url'];
   echo '<pre>';
       print_r($url);
   echo '</pre>';
?>

NOTE: you can call explode function on $url variable

rewrite module must be on in Apache server.

enter image description here

Note i think this will solve your problem

script is self explanatory but if confusion then tell me .

<?php
  $url = $_GET['url'];

  echo '<pre>';
      print_r($url);
  echo '</pre>';

  //echo '<pre>';
      ///print_r(explode('/',$url,4));
  //echo '</pre>';

  list($file,$controller,$method,$params) = explode('/',$url,4);


  echo "<hr/>file:" . $file;
  echo "<hr/>controller:" . $controller;
  echo "<hr/>method:" . $method;

  echo "<hr/>params:" ;
  echo '<pre>';
      print_r($params);
  echo '</pre>';

  ?>

Output 2 enter image description here

Er. Amit Joshi
  • 611
  • 5
  • 21
  • @mike-runac support back if it really helped then mark as answer and thumb. – Er. Amit Joshi Sep 18 '18 at 08:38
  • Passing everything through as an URL parameter is a bit *old skool* - you'd normally redirect everything that's not an existing file/directory/(or symlink possibly) to the routing script that checks `$_SERVER['REQUEST_URI']` to work out what action to perform. – CD001 Sep 18 '18 at 08:43
  • but old school tricks always problem solver. :) BTW you way is also right – Er. Amit Joshi Sep 18 '18 at 08:50
0

You can find all you are looking for in the $_SERVER variable (php.net) Just print_r the value of $_SERVER and see what information is there... You will see quite easy what you will need. You can then use string functions to isolate the part you need in the URL or query string to fetch whatever you need.

Also...

If a .htaccess is used with rewrites, they will probably be sent in the $_GET variables. To be sure, you need to post some of your .htaccess where the rewrites are being done.

MrG
  • 372
  • 2
  • 13