Adding Rest Service to MYSQL Database

Having a website provider or a wordpress hosting plan, will typically comes with a PHP MyAdmin setup configured.  With the PHP MyAdmin, you will have mySQL Database setup along with it.

I needed a way to insert records into the database with an API from a CRON job.

Therefore, I have come to the following solution.

Step 1.  Create an API user for the database.

You will need access to the SQL Database or either via the PHP myAdmin GUI.

The objective here is to make sure that the user account only has read and insert access and not the rest of the permission.

mySQL API User Access
mySQL API User Access

 

Step 2.  Download the following Github Code from https://github.com/mevdschee/php-crud-api

We will be using just 1 particular file from this code depository, which is the API.php

Get to the end of the file, look for the following section $config = … , update that with the API username, password and the database instance name.

API Config User
API Config User

Step 3. You will need to upload the file to your webserver.

Upload the file to public_html folder first to make sure that you are able to access it before moving it.

Step 4. Testing

To test if api access is working, do the following:

http://<your domain>.com/api.php/records/<your table>

This should return a json dataset with all the records in the table.

Finally, if you are looking to insert data to the table, just post the json body using the same URL, instead of GET do a POST

Leave a Reply

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