4

I have a mysql database on a server and I insert data on it using a script I have written. I have to open www.mywebsite.com/rest/create.php?param=value&param2=value2 to create a new resource, then I have to open another php file to edit an item an so on. This is the basic approach: a php file takes some parameters and then it stores files on a database.

I am using TIdHTTP because I have the possibility to do something like:

TIdHTTP1.Get(www.mywebsite.com/rest/create.php?param=value&param2=value2);

The create.php page is returning some data encoded in json. In my android app I have created a rest client that is able to parse the json received in response to the php page. For example, if I called .../show.php?id=3 the result of the page is a json with my data; later the android app will parse them and show some results to the user.


Question

Instead of calling www.mywebsite.com/rest/create.php?param=value&param2=value2 I would like to be able to do something like this www.mywebsite.com/rest/create/?param=value&param2=value2. Basically I dont want to use the filename in *.php but I want to use the 2nd kind of URI without the filename.

The problem is that it's my first time and I don't really know what to do.

Possible solution. After reading Expert Delphi I have found a possible solution. I could create a WebBroker server for linux (so that I can export the Apache module *.so), implement some actions like this:

enter image description here

When there is a request to /rest/create/ (using the OnAction event) I can run a TIdHTTP1.Get(www.mywebsite.com/rest/create.php?param=value&param2=value2); and store the result. In this way I can open the www.mywebsite.com/rest/create/ as I need and save the data. Can indy execute a get request even if it is on a linux machine (as apache module)? I have seen that the standalone server runs under indy so I guess that indy should be able to work on an apache module.

Unfortunately I am pretty new with this kind of things, actually I also don't really know what to search online to find a solution. My solution could work but I am pretty sure that there is a proper end efficient way to do this.

Note: this is just an idea after some researches I have made and I am not sure this is the proper way to do what I need. Instead of doing what I've written above, do I simply have to do something with PHP or Apache? Is there another standard (and best) way to create a REST service?

5
  • I guess the most common way would be to rewrite the uri. If you use Apache then look for mod_rewrite. Commented Aug 28, 2017 at 5:33
  • 1
    Indy includes a HTTP Server component (TIdHTTPServer) which you can use in a standalone executable (no Apache module), which makes testing easy. Then, for additional features such as HTTPS and URL rewriting, you can use the Apache HTTP server as reverse proxy. (I did this with Indy and Free Pascal, not with Delphi) Commented Aug 28, 2017 at 6:52
  • @mjn42 Thank you for the help :) So basically I can create the server and everything but at the end I have to use Apache to setup proper urls. Apache is the one that manages URIs right? Commented Aug 28, 2017 at 6:59
  • TIdHTTPServer is able to server different content on different URI paths, as the path is included in the HTTP header. Apache is helpful for port and path mappings for example if you have multiple local HTTP services running (on localhost:8080, localhost:8081 etc.), which need to be mapped to 443, on different URI paths). Commented Aug 28, 2017 at 9:59
  • Have you heard of Delphi's Datasnap? You can make Rest service with that. Commented Aug 28, 2017 at 10:27

1 Answer 1

2

Your problem is not on Delphi side but on server one.

If you want a URL /rest/create/, you simply have to create a folder "create" under "rest" folder on your server and put an index.php file on it.

Or you can use URL rewriting rules in a .htaccess file.

Sign up to request clarification or add additional context in comments.

5 Comments

Is this how rest services are implemented? Just a folder hierarchy with a index.php file? Thank you anyway! :)
HTTP paths are not related to file system locations. They are independent. Maybe PHP is limited in this way, but it is not the case in general.
PHP has no limit, it depends on the complexity you have in your architecture. Sometime we only have an index.php at root and url rewriting to dispatch paths. Sometimes your have simple directories. It's the easier way when you start programming this or for little API projects.
@PatrickPrémartin thank you, I guess I am looking for url rewriting. Just to know, how do you structure that? Do you keep all the php files in a single folder and then you use url rewriting to access them?
It depends on the complexity of the project, the framework used if any. I can't give you a rule for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.