Get Bank details from IFSC Code Using PHP
The Indian Financial System Code (IFSC) is an 11-digit alpha-numeric code used to uniquely classify bank branches within the National Electronic Fund Transfer (NEFT) network by the Central Bank.
In this article, we are going to write PHP code to get details of the Bank from the given IFSC code. For this, we will be using Razorpay’s IFSC API which is an API server that serves Razorpay’s IFSC API. The link of the API is https://ifsc.razorpay.com/. After “/” we have to give the IFSC code of the bank. It will give all the details in JSON format.
Example: KARB0000001 is the IFSC code of the bank in Karnataka. When we enter the URL https://ifsc.razorpay.com/KARB0000001 it will return the details of that particular bank in JSON Format.
The details include Bank Name, Branch, Address, Contact, etc.
HTML Code: The following demonstrates the implementation using HTML and PHP code for the above discussion. We make a simple HTML form with an input control and a submit button. In the input control, we have to enter the IFSC code of the bank and submit the form. After submission of the form, we will get the details of the bank.
PHP Code: When the user submits the form, we store the IFSC code in a variable using the PHP $_POST. PHP $_POST is a superglobal variable that is used to collect form data after submitting an HTML form with method=”post”. After that we use file_get_contents() method to read the content of the file storing in a variable. As we get the data in JSON format, we first convert it into an array. For that, we are using json_decode() function. The json_decode() function is used to decode or convert a JSON object to a PHP object. Now we can easily parse the array object using the array operator and show the details to the user.
Suppose the user enters an incorrect IFSC code then some error messages will display which is not understandable to the user. To this problem, we use the if() condition checking if we get some data. We can pass any parameter in the if() condition that is present in our array. For example, if we use “Branch”, after parsing the “Branch” we get some value, it means that the IFSC code is correct else its incorrect. Instead of the “Branch”, we can also use the “Bank” or “Address”, etc. If the IFSC code is correct details will be shown on screen otherwise a “Invalid IFSC Code” message will be shown.