Build a Simple REST API in PHP
Learn how to build a simple REST API with authentication in PHP.
Build a Simple REST API in PHP
Learn how to build a simple REST API with authentication in PHP.
Mindaugas
VERY BAD EXAMPLE. DO NOT SPREAD IT!
Tim
I see you mentioned an .env.example file.
Is this a standard format, or can I use (say) a .env.app file as well?
aaronpk
I don’t know what a .env.app file is, but the .env.example file is just a text file, and it has to be copied to .env once you fill out the values.
Eze Sunday Eze
You can use anything. You could use something like .env.prod
.env.dev
Prashant Mishra
Where is src folder ?
src/System/DatabaseConnector.php
Where i have to add DatabaseConnector.php ?
robert sageme
Is the URL on the sample client supposed to be as shown above or you have to change it to match your values?
Rune Jensen Heidtmann
Hi if im using XAMPP and placing this app in htdocs/api, the app will not run on localhost/api/person
Where should i place the app in such a setup?
Viktor Gavrilovic
How to define the routes?
Zahidul Islam
src is a folder that you make ex. mkdir src
Manny
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn’t exist, use index.html
RewriteRule ^ /path/public/index.php
Rupesh Jain
Here we are serving the API using the PHP’s build in web server. What if I want to serve using Apache? Can you please help.
Julian Conway
You’d need to use mod_rewrite to redirect uri through the index.php. Add a .htaccess file to the directory your index.php is in with the following content (may need to be adjusted to suit your environment):
<ifmodule mod_rewrite.c="">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
</ifmodule>
peter haworth
Thanks for this. I assume there must be some server configuration changes need to make sure all the api calls go to the index.php script. But even then, wouldn’t the server throw an error because it would look for a file/directory named /person?
Krasimir Hristozov
No, the index.php file serves as a Front Controller and it parses the URI and handles the routing
Krasimir Hristozov
The routes are served by the index.php file which acts as a front controller - it parses the URI and handles the routing automatically. Just make sure to pass all requests to it (when using the built-in web server, this is not a problem).
Krasimir Hristozov
The sample client can be run from the command line as per the tutorial, it doesn’t have a URL (but I suppose there’s no problem to run it through the Web server, if you prefer).
Mohd Fariz Ismail
Trying to connect to 127.0.0.1:8000 with Postman, and I get this error: ‘SQLSTATE[HY000] [2002] No such file or directory’.
I’ve tried to connect it using my Browser(Chrome), by pasting the same exact URL, but It still doesn’t work. I’ve assumed that the path and files should be located correctly, as in the example above, the ‘index.php’ file should be under the ‘public’ folder, right?
Is there any other issues? My Project root folder named ‘NoteCast’, where under it there are the ‘public’ folder. Does this seems ok?
Thanks for help.
Mohd Fariz Ismail
Ok, I’ve found the solution myself, it’s actually a SQL connection issue. I’m using a MAMP for Mac, so there could be a mysql.sock filepath settings. Now it could run smoothly.
But, at the end, the messages coming out from the console are as follow:
(from the terminal of my macBook pro):
Obtaining token…failed, exiting.farizs-MacBook-Pro:public farizismail$ php client.php
Obtaining token…success!
Getting all users…string(249) "
Deprecated: filter_var(): explicit use of FILTER_FLAG_SCHEME_REQUIRED and FILTER_FLAG_HOST_REQUIRED is deprecated in /usr/local/var/www/NoteCast/vendor/spomky-labs/jose/src/Object/DownloadedJWKSet.php on line 65
"
Getting user with id#1…string(247) "
Deprecated: filter_var(): explicit use of FILTER_FLAG_SCHEME_REQUIRED and FILTER_FLAG_HOST_REQUIRED is deprecated in /usr/local/var/www/NoteCast/vendor/spomky-labs/jose/src/Object/DownloadedJWKSet.php on line 65
"
is there any issues when it says ‘explicit use of FILTER_FLAG_SCHEME_REQUIRED’ as above? do i need to upgrade/replace whichever version used in this tutorial as needed?
Thanks for the advice!
Ken Cole
I do not get ow to run this through a normal web server. I tried all the suggestions below about .htaccess etc. but could not get any to work. What URI would I use if testing say on localhost?