Get URL ID instead of INDEX

2

Hello, I would like to know how to get the URL ID as if the index was the ID, so type Using GET:

http://domain.com/api.php?id=1999&output=json
$id = $_GET['id'];

Okay, so long, well I'd like to know how you can get the id like this:

http://domain.com/1999/api.php?output=json

I would like to know how to get this ID, which in this case would be 1999 .

I want my index to detect in a way, a great example is the API of PagSeguro, that link or of the Paid Market:

http[s]://api.mercadopago.com/collections/notifications/{id here}?access_token={token here}

Example:

http[s]://api.mercadopago.com/collections/notifications/1799030657?access_token=APP_USR-46....

The output would look something like this:

    
asked by anonymous 07.02.2016 / 22:04

2 answers

3

You should change this in htaccess , for example:

Changes:

HTACCESS:

  

/public_html/.htaccess

RewriteEngine On
RewriteRule ^([0-9]+)/api.php?$ api.php?id=$1$2 [QSA]

PHP:

  

/public_html/api.php

<?php
$id = $_GET['id'];
$output = $_GET['output'];
?>

Explanation:

RewriteRule will "read the url" but will point somewhere else, I think it will be easy to understand.

Typing the URL:

http://domain.com/1999/api.php?output=json

Will call the file:

api.php?id=1999&output=json

This will NOT REDIRECT USER, but you can also redirect it.

Construction:

If you do not understand RewriteEngine, here is a quick step-by-step, to find out how this was done, at least by me:

RewriteEngine On
# Liga esta função

RewriteRule ^([0-9]+)/api.php?$ api.php?id=$1$2 [QSA]
#
# Primeira parte:
# ([0-9]+) será o 1999 (http://domain.com/1999)
# /api.php será para restringir (http://domain.com/1999/api.php)
# 
# Segunda parte:
# api.php será o arquivo chamado
# ?id=$1 será o resultado do ([0-9]+) (?id=1999)
# $2 [QSA] será o resto do query/parametros 
    
08.02.2016 / 03:21
1

You can use explode('/', $url) :

$url = 'http://domain.com/1999/api.php?output=json'; 
$r = explode('/', $url);
$id = $r[3];
echo $id;

link

    
08.02.2016 / 01:09