RESTful API With PHP & MySQL



In this video we will build a complete JSON RESTful API using PHP and MySQL. We will use the SlimPHP framework along with PDO to interact with the MySQL database.

CODE -https://github.com/bradtraversy/slimapp

RELATED COURSE – http://www.traversymedia.com/course/learn-top-ten-frameworks-in-php-by-building-projects/

SlimPHP Site – https://www.slimframework.com/

Get Composer – https://getcomposer.org/

43 thoughts on “RESTful API With PHP & MySQL

  1. Excellent tutorial.
    Note to others though: security here is bad, this code is extremely vulnerable to SQL injection.
    You should use prepared statements for any parameterised query, not just PUT/POST requests and the inputs should also be validated before being used in SQL statement.

  2. I followed along with this but did it in Laravel so I could get to work on the Front End part of this project. Thank you for introducing me to SlimPHP though, I can see that I will use it for simple PHP tasks where a framework like Laravel is overkill.

  3. Hi there, any help would be appreciated, I'm getting the following error.

    Fatal error: Uncaught Error: Call to a member function query() on null in..

    From my research it seems like a scope problem. for some reason $db is null when I attempt to call query on it, any advice?

  4. Fatal error: Uncaught SlimExceptionHttpNotFoundException: Not found. in C:xampphtdocsmyappvendorslimslimSlimMiddlewareRoutingMiddleware.php:93 Stack trace: #0 C:xampphtdocsmyappvendorslimslimSlimRoutingRouteRunner.php(72): SlimMiddlewareRoutingMiddleware->performRouting(Object(SlimPsr7Request)) #1 C:xampphtdocsmyappvendorslimslimSlimMiddlewareDispatcher.php(81): SlimRoutingRouteRunner->handle(Object(SlimPsr7Request)) #2 C:xampphtdocsmyappvendorslimslimSlimApp.php(215): SlimMiddlewareDispatcher->handle(Object(SlimPsr7Request)) #3 C:xampphtdocsmyappvendorslimslimSlimApp.php(199): SlimApp->handle(Object(SlimPsr7Request)) #4 C:xampphtdocsmyapppublicindex.php(18): SlimApp->run() #5 {main} thrown in C:xampphtdocsmyappvendorslimslimSlimMiddlewareRoutingMiddleware.php on line 93

  5. That was very helpfull, this was my first time doing an api with php and I loved it, thank you.
    I know this is from 2016 but if you're reading this maybe you could try Insomnia, its an amazing program to try GET/REST http request.

  6. Good overview! Is it normal that new SlimApp is called 2 times in index.php and in customers.php ? (looks strange to me)
    We are missing the sanitization of 'id' in SQL requests. We also don't check that the input parameters are well supplied in API requests.

  7. I am a fan of the channel, but gotta admit that this tutorial is not so good. You could have used namespaces and controllers to handle business logic. Also, instead of PDO, something more robust like Eloquent ORM to work with Database.

  8. Brad, I've beed through all these tutorials, but still couldn't find how can I use this backend in the frontend. I always see tutorials with frontend and javascript and backend without frontend. How can I use this data with eg. angular? Thank you.

  9. Hi, so far, I have gotten to the point of POST request and adding a customer to the database via the REST client. I am using Firefox Developer Edition, and am using RESTer client.

    The issue I am having is that I cannot make a POST request. Firstly, in the code, there seems to be a problem with the $app->post() URL fragment, in that 'add' is a reserved PHP7 word and back ticks will not permit it to be used.

    Second, I get a "405 Method Not Allowed" in the REST client. I set the Method to POST and Header to Content-Type: application/json, but it seems I require something else, possibly authorization tokens. I have done my best to research this, but I cannot find anything other than utilising third party apps and writing code to incorporate it. Can anyone help?
    EDIT:
    I changed the the last part of the $app->post() path to "customer_add", which solved the problem; a new record was added to the database.

Leave a Reply

Your email address will not be published. Required fields are marked *