⊗ppSpAiHRU 51 of 83 menu

API with Human-Readable URLs in PHP

Let's now make API addresses in the form of human-readable URLs. For example, we will pass the first and second number:

http://api.loc/1/100/

To implement human-readable URLs, first we will create an htaccess file, in which we will send all requests to index.php:

RewriteRule .+ index.php

In the index.php file, we will get the requested URI:

<?php $uri = $_SERVER['REQUEST_URI']; ?>

After receiving the URI, you can parse the parameters and display a random number in the specified range:

<?php preg_match('#^/([0-9]+)/([0-9]+)/?$#', $uri, $match); if (isset($match[1]) and isset($match[2])) { echo mt_rand($match[1], $match[2]); } else { echo 'error'; } ?>

Implement an API for working with years, according to the behavior described below.

Checks if a year is a leap year:

http://api.loc/leap/2025/

Returns the difference between years:

http://api.loc/diff/2025/2030/
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline