Get Bank details from IFSC Code Using PHP - GeeksforGeeks

10 Dec, 2020
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.
HTML
<!DOCTYPE html><html>  <body>    <form method="post" action="index.php" id="theForm">        <b>Enter IFSC Code:</b>        <input type="text" name='ifsc'>        <input type="submit" id="formSubmit">    </form></body>  </html>
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.
PHP
<?phpif(isset($_POST['ifsc'])) {    $ifsc = $_POST['ifsc'];    $json = @file_get_contents(        "https://ifsc.razorpay.com/".$ifsc);    $arr = json_decode($json);      if(isset($arr->BRANCH)) {        echo '<pre>';        echo "<b>Bank:-</b>".$arr->BANK;        echo "<br/>";        echo "<b>Branch:-</b>".$arr->BRANCH;        echo "<br/>";        echo "<b>Centre:-</b>".$arr->CENTRE;        echo "<br/>";        echo "<b>District:-</b>".$arr->DISTRICT;        echo "<br/>";        echo "<b>State:-</b>".$arr->STATE;        echo "<br/>";        echo "<b/>Address:-</b>".$arr->ADDRESS;        echo "<br/>";    }    else {        echo "Invalid IFSC Code";    }}?>
Output:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
HTML-Misc
PHP-Misc
Technical Scripter 2020
HTML
PHP
PHP Programs
Technical Scripter
Web Technologies
HTML
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
REST API (Introduction)
How to Insert Form Data into Database using PHP ?
CSS to put icon inside an input element in a form
Types of CSS (Cascading Style Sheet)
How to execute PHP code using command line ?
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
PHP in_array() Function
How to pop an alert message box using PHP ?