Get URL ID instead of INDEX

Asked

Viewed 2,426 times

2

Hi, I’d like to know how to get the URL ID as if the index were the ID, like this Using GET:

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

Okay, so far so good, but I’d like to know how it does to get the id like this:

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

I wonder how you get this ID that in case it would be 1999.


I want my index detect so, a great example is the Pagseguro API, that link or the Mercado Pago:

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:

inserir a descrição da imagem aqui

  • 1

    You changed the url with . htaccess, right?

  • It depends on what you are talking about friend, I changed more I believe that is not what you are quoting, could give me an example friend?

2 answers

3


Should change that in the htaccess, for example:

Amendments:

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 to another place, I believe so it will be easy to understand.

Typing in the URL:

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

Will call the court:

api.php?id=1999&output=json

This will NOT REDIRECT THE USER, but it is possible to do also redirecting.

Building:

In case you don’t understand Rewriteengine, here’s a brief step-by-step, to know how it 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 
  • Thank you very much friend.

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.