Show
Demo Download In this tutorial, we will create and consume simple REST API in PHP. REST enables you to access and work with web based services. But before moving ahead let me explain what is REST and how does it work. What is REST?REST stands for Representational State Transfer, REST is an architectural style which defines a set of constraints for developing and consuming web services through standard protocol (HTTP). REST API is a simple, easy to implement and stateless web service. There is another web service available which is SOAP which stands for Simple Object Access Protocol which is created by Microsoft. REST API is widely used in web and mobile applications as compared to SOAP. REST can provide output data in multiple formats such as JavaScript Object Notation (JSON), Extensible Markup Language (XML), Command Separated Value (CSV) and many others while SOAP described output in Web Services Description Language (WSDL). How Does REST API WorkREST requests are related to CRUD operations (Create, Read, Update, Delete) in database, REST uses GET, POST, PUT and DELETE requests. Let me compare them with CRUD.
JSON format is the most common output format of REST API, we will use the JSON format to consume our simple REST API. We will developed an online transaction payment REST API for our example. I will try to keep it as simple as possible so i will use GET request to retrieve information.
1. Create REST API in PHPTo create a REST API, follow these steps:
1. Create a Database and Table with Dummy DataTo create database run the following query.
To create a table run the following query. Note: I have already attached the SQL file of this table with dummy data, just download the complete zip file of this tutorial.
2. Create a Database ConnectionJust create a db.php file and paste the following database connection in it. Make sure that you update these credentials with your database credentials.
3. Create a REST API FileCreate a api.php file and paste the following script in it.
The above script will accept the GET request and return output in the JSON format. I have created all these files in folder name rest, now you can get the transaction information by browsing the following URL.
You will get the following output. Above URL is not user friendly, therefore we will rewrite URL through the .htaccess file, copy paste the following rule in .htaccess file.
Now you can get the transaction information by browsing the following URL.
You will get the following output. 2. Consume REST API in PHPTo consume a REST API, follow these steps:
1. Create an Index File with HTML Form
2. Fetch Records through CURL
You can do anything with these output data, you can insert or update it into your own database if you are using REST API of any other service provider. Usually in case of online transaction, the service provider provides status of payment via API. You can check either payment is made successfully or not. They also provide a complete guide of it. Make sure CURL is enabled on your web server or on your localhost when you are testing demo. I try my best to explain this tutorial as simple as possible. Demo Download If you found this tutorial helpful, share it with your friends and developers group. I spent several hours to create this tutorial, if you want to say thanks so like my page on Facebook and share it. Facebook Official Page: All PHP Tricks How create MySQL API in PHP?In this step we will create PHP REST API Endpoints to insert or add a single record in MySQL table.
...
Create create. php file in the api folder and add the following code.. Can we create API using PHP?There are many great frameworks that can help you build REST APIs quickly. Laravel/Lumen and Symfony's API platform are the most often used examples in the PHP ecosystem. They provide great tools to process requests and generate JSON responses with the correct HTTP status codes.
Can we call API from MySQL?The only way MySQL can call a webservice is through a custom UDF. See the below link, but basically what you would need to do is build a C program that would call the remote webservice API with the variables as parameters, return the reponse via the UDF to your actual SQL which could insert the results to a table.
Can I use REST API in PHP?We can make the JSON we retrieved using our REST call in php to the javascript. During the initial page load, this json will be passed in the DOM. Client side rendering is faster as it can avoid the HTTP call to fetch the JSON.
|