Search code examples
phpesi

How to receive GET request from URL


I am used to SEND all sorts of requests to 'foreign' URLs or to my web site Server URL and receive responses and such.

And thus, I am used to receive requests as well.

BUT

Only if it targets a PHP file like so : http://www.example.com/file.php

In order for a core part of my future web site feature to work, I need to be able to listen to a GET request made to a "blank" URL like so : http://www.example.com/blank/?key1=value1&key2=value2

Is there a way to 'run' a PHP script whenever this URL is called? Even if it is not targeted directly in the request? Any other script?

I'm ignorant when it comes to server-side mechanisms.

EDIT :

I have tried to create a file.PHP with the following :

$key1 = $_GET['key1'];
$key2 = $_GET['key2'];
echo "$key1 and $key2";

but response is 403 (forbidden)


EDIT2 : To my moderator : the question linked is not the exact same question as mine. He asks to hide the file extension, I ask how to target a directory. Even if the answer has the same source (htaccess) it is not the same thought process.
Further more, my title explicitly mentions requests (hence the tags API and ESI) and not only the PHP subject.


Solution

  • In general: Yes but how to do it exactly depends on your web server software. For Apache for example, you can use a .htaccess file to create rewrite rules to make paths matching a certain pattern (e.g. /blank/<whatever>) behave as if they were another path (e.g. /blank.php).

    But in your case where you actually have /blank/ as "directory", there is an even simpler way: Put an index.php file into the directory.

    It's an almost universal convention that if a directory is specified in the URL (without specific filename), the server will look for an index.php file (or index.html, but your question was about PHP in particular) in the directory and direct the request there.