PHP - Convert Currency With Currency API

Submitted by: 
Language: 
Visitors have accessed this post 213 times.

In this tutorial we will try to create a Convert Currency With Currency API. jQuery is a cross-platform javaScript library design to simplified the modern HTML client side scripting. jQuery syntax was made to make it easier to navigate a document, like select DOM elements, develop Ajax applications and etc. So let's start coding...

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Convert Currency With Currency API</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-2"></div>
  19. <div class="col-md-8">
  20. <div class="alert alert-info">Currency Converter</div>
  21. <form>
  22. <div class="form-group">
  23. <label>Enter Ammount:</label>
  24. <input type="number" class="form-control" id="amount"/>
  25. </div>
  26. <div class="form-inline pull-left">
  27. <label>FROM</label>
  28. <br />
  29. <select id="from" class="form-control">
  30. <option value="AED">AED</option>
  31. <option value="AFN">AFN</option>
  32. <option value="ALL">ALL</option>
  33. <option value="AMD">AMD</option>
  34. <option value="ANG">ANG</option>
  35. <option value="AOA">AOA</option>
  36. <option value="ARS">ARS</option>
  37. <option value="AUD">AUD</option>
  38. <option value="AWG">AWG</option>
  39. <option value="AZN">AZN</option>
  40. <option value="BAM">BAM</option>
  41. <option value="BBD">BBD</option>
  42. <option value="BDT">BDT</option>
  43. <option value="BGN">BGN</option>
  44. <option value="BHD">BHD</option>
  45. <option value="BIF">BIF</option>
  46. <option value="BND">BND</option>
  47. <option value="BOB">BOB</option>
  48. <option value="BRL">BRL</option>
  49. <option value="BSD">BSD</option>
  50. <option value="BTC">BTC</option>
  51. <option value="BTN">BTN</option>
  52. <option value="BWP">BWP</option>
  53. <option value="BYN">BYN</option>
  54. <option value="BYR">BYR</option>
  55. <option value="BZD">BZD</option>
  56. <option value="CAD">CAD</option>
  57. <option value="CDF">CDF</option>
  58. <option value="CHF">CHF</option>
  59. <option value="CLP">CLP</option>
  60. <option value="CNY">CNY</option>
  61. <option value="COP">COP</option>
  62. <option value="CRC">CRC</option>
  63. <option value="CUP">CUP</option>
  64. <option value="CVE">CVE</option>
  65. <option value="CZK">CZK</option>
  66. <option value="DJF">DJF</option>
  67. <option value="DKK">DKK</option>
  68. <option value="DOP">DOP</option>
  69. <option value="DZD">DZD</option>
  70. <option value="EGP">EGP</option>
  71. <option value="ERN">ERN</option>
  72. <option value="ETB">ETB</option>
  73. <option value="EUR">EUR</option>
  74. <option value="FJD">FJD</option>
  75. <option value="FKP">FKP</option>
  76. <option value="GBP">GBP</option>
  77. <option value="GEL">GEL</option>
  78. <option value="GHS">GHS</option>
  79. <option value="GIP">GIP</option>
  80. <option value="GMD">GMD</option>
  81. <option value="GNF">GNF</option>
  82. <option value="GTQ">GTQ</option>
  83. <option value="GYD">GYD</option>
  84. <option value="HKD">HKD</option>
  85. <option value="HNL">HNL</option>
  86. <option value="HRK">HRK</option>
  87. <option value="HTG">HTG</option>
  88. <option value="HUF">HUF</option>
  89. <option value="IDR">IDR</option>
  90. <option value="ILS">ILS</option>
  91. <option value="INR">INR</option>
  92. <option value="IQD">IQD</option>
  93. <option value="IRR">IRR</option>
  94. <option value="ISK">ISK</option>
  95. <option value="JMD">JMD</option>
  96. <option value="JOD">JOD</option>
  97. <option value="JPY">JPY</option>
  98. <option value="KES">KES</option>
  99. <option value="KGS">KGS</option>
  100. <option value="KHR">KHR</option>
  101. <option value="KMF">KMF</option>
  102. <option value="KPW">KPW</option>
  103. <option value="KRW">KRW</option>
  104. <option value="KWD">KWD</option>
  105. <option value="KYD">KYD</option>
  106. <option value="KZT">KZT</option>
  107. <option value="LAK">LAK</option>
  108. <option value="LBP">LBP</option>
  109. <option value="LKR">LKR</option>
  110. <option value="LRD">LRD</option>
  111. <option value="LSL">LSL</option>
  112. <option value="LVL">LVL</option>
  113. <option value="LYD">LYD</option>
  114. <option value="MAD">MAD</option>
  115. <option value="MDL">MDL</option>
  116. <option value="MGA">MGA</option>
  117. <option value="MKD">MKD</option>
  118. <option value="MMK">MMK</option>
  119. <option value="MNT">MNT</option>
  120. <option value="MOP">MOP</option>
  121. <option value="MRO">MRO</option>
  122. <option value="MUR">MUR</option>
  123. <option value="MVR">MVR</option>
  124. <option value="MWK">MWK</option>
  125. <option value="MXN">MXN</option>
  126. <option value="MYR">MYR</option>
  127. <option value="MZN">MZN</option>
  128. <option value="NAD">NAD</option>
  129. <option value="NGN">NGN</option>
  130. <option value="NIO">NIO</option>
  131. <option value="NOK">NOK</option>
  132. <option value="NPR">NPR</option>
  133. <option value="NZD">NZD</option>
  134. <option value="OMR">OMR</option>
  135. <option value="PAB">PAB</option>
  136. <option value="PEN">PEN</option>
  137. <option value="PGK">PGK</option>
  138. <option value="PHP">PHP</option>
  139. <option value="PKR">PKR</option>
  140. <option value="PLN">PLN</option>
  141. <option value="PYG">PYG</option>
  142. <option value="QAR">QAR</option>
  143. <option value="RON">RON</option>
  144. <option value="RSD">RSD</option>
  145. <option value="RUB">RUB</option>
  146. <option value="RWF">RWF</option>
  147. <option value="SAR">SAR</option>
  148. <option value="SBD">SBD</option>
  149. <option value="SCR">SCR</option>
  150. <option value="SDG">SDG</option>
  151. <option value="SEK">SEK</option>
  152. <option value="SGD">SGD</option>
  153. <option value="SHP">SHP</option>
  154. <option value="SLL">SLL</option>
  155. <option value="SOS">SOS</option>
  156. <option value="SRD">SRD</option>
  157. <option value="STD">STD</option>
  158. <option value="SYP">SYP</option>
  159. <option value="SZL">SZL</option>
  160. <option value="THB">THB</option>
  161. <option value="TJS">TJS</option>
  162. <option value="TMT">TMT</option>
  163. <option value="TND">TND</option>
  164. <option value="TOP">TOP</option>
  165. <option value="TRY">TRY</option>
  166. <option value="TTD">TTD</option>
  167. <option value="TWD">TWD</option>
  168. <option value="TZS">TZS</option>
  169. <option value="UAH">UAH</option>
  170. <option value="UGX">UGX</option>
  171. <option value="USD">USD</option>
  172. <option value="UYU">UYU</option>
  173. <option value="UZS">UZS</option>
  174. <option value="VEF">VEF</option>
  175. <option value="VND">VND</option>
  176. <option value="VUV">VUV</option>
  177. <option value="WST">WST</option>
  178. <option value="XAF">XAF</option>
  179. <option value="XCD">XCD</option>
  180. <option value="XDR">XDR</option>
  181. <option value="XOF">XOF</option>
  182. <option value="XPF">XPF</option>
  183. <option value="YER">YER</option>
  184. <option value="ZAR">ZAR</option>
  185. <option value="ZMW">ZMW</option>
  186. </select>
  187. </div>
  188. <div class="form-inline pull-left" style="margin-left:30%;">
  189. <label>TO</label>
  190. <br />
  191. <select id="to" class="form-control">
  192. <option value="AED">AED</option>
  193. <option value="AFN">AFN</option>
  194. <option value="ALL">ALL</option>
  195. <option value="AMD">AMD</option>
  196. <option value="ANG">ANG</option>
  197. <option value="AOA">AOA</option>
  198. <option value="ARS">ARS</option>
  199. <option value="AUD">AUD</option>
  200. <option value="AWG">AWG</option>
  201. <option value="AZN">AZN</option>
  202. <option value="BAM">BAM</option>
  203. <option value="BBD">BBD</option>
  204. <option value="BDT">BDT</option>
  205. <option value="BGN">BGN</option>
  206. <option value="BHD">BHD</option>
  207. <option value="BIF">BIF</option>
  208. <option value="BND">BND</option>
  209. <option value="BOB">BOB</option>
  210. <option value="BRL">BRL</option>
  211. <option value="BSD">BSD</option>
  212. <option value="BTC">BTC</option>
  213. <option value="BTN">BTN</option>
  214. <option value="BWP">BWP</option>
  215. <option value="BYN">BYN</option>
  216. <option value="BYR">BYR</option>
  217. <option value="BZD">BZD</option>
  218. <option value="CAD">CAD</option>
  219. <option value="CDF">CDF</option>
  220. <option value="CHF">CHF</option>
  221. <option value="CLP">CLP</option>
  222. <option value="CNY">CNY</option>
  223. <option value="COP">COP</option>
  224. <option value="CRC">CRC</option>
  225. <option value="CUP">CUP</option>
  226. <option value="CVE">CVE</option>
  227. <option value="CZK">CZK</option>
  228. <option value="DJF">DJF</option>
  229. <option value="DKK">DKK</option>
  230. <option value="DOP">DOP</option>
  231. <option value="DZD">DZD</option>
  232. <option value="EGP">EGP</option>
  233. <option value="ERN">ERN</option>
  234. <option value="ETB">ETB</option>
  235. <option value="EUR">EUR</option>
  236. <option value="FJD">FJD</option>
  237. <option value="FKP">FKP</option>
  238. <option value="GBP">GBP</option>
  239. <option value="GEL">GEL</option>
  240. <option value="GHS">GHS</option>
  241. <option value="GIP">GIP</option>
  242. <option value="GMD">GMD</option>
  243. <option value="GNF">GNF</option>
  244. <option value="GTQ">GTQ</option>
  245. <option value="GYD">GYD</option>
  246. <option value="HKD">HKD</option>
  247. <option value="HNL">HNL</option>
  248. <option value="HRK">HRK</option>
  249. <option value="HTG">HTG</option>
  250. <option value="HUF">HUF</option>
  251. <option value="IDR">IDR</option>
  252. <option value="ILS">ILS</option>
  253. <option value="INR">INR</option>
  254. <option value="IQD">IQD</option>
  255. <option value="IRR">IRR</option>
  256. <option value="ISK">ISK</option>
  257. <option value="JMD">JMD</option>
  258. <option value="JOD">JOD</option>
  259. <option value="JPY">JPY</option>
  260. <option value="KES">KES</option>
  261. <option value="KGS">KGS</option>
  262. <option value="KHR">KHR</option>
  263. <option value="KMF">KMF</option>
  264. <option value="KPW">KPW</option>
  265. <option value="KRW">KRW</option>
  266. <option value="KWD">KWD</option>
  267. <option value="KYD">KYD</option>
  268. <option value="KZT">KZT</option>
  269. <option value="LAK">LAK</option>
  270. <option value="LBP">LBP</option>
  271. <option value="LKR">LKR</option>
  272. <option value="LRD">LRD</option>
  273. <option value="LSL">LSL</option>
  274. <option value="LVL">LVL</option>
  275. <option value="LYD">LYD</option>
  276. <option value="MAD">MAD</option>
  277. <option value="MDL">MDL</option>
  278. <option value="MGA">MGA</option>
  279. <option value="MKD">MKD</option>
  280. <option value="MMK">MMK</option>
  281. <option value="MNT">MNT</option>
  282. <option value="MOP">MOP</option>
  283. <option value="MRO">MRO</option>
  284. <option value="MUR">MUR</option>
  285. <option value="MVR">MVR</option>
  286. <option value="MWK">MWK</option>
  287. <option value="MXN">MXN</option>
  288. <option value="MYR">MYR</option>
  289. <option value="MZN">MZN</option>
  290. <option value="NAD">NAD</option>
  291. <option value="NGN">NGN</option>
  292. <option value="NIO">NIO</option>
  293. <option value="NOK">NOK</option>
  294. <option value="NPR">NPR</option>
  295. <option value="NZD">NZD</option>
  296. <option value="OMR">OMR</option>
  297. <option value="PAB">PAB</option>
  298. <option value="PEN">PEN</option>
  299. <option value="PGK">PGK</option>
  300. <option value="PHP">PHP</option>
  301. <option value="PKR">PKR</option>
  302. <option value="PLN">PLN</option>
  303. <option value="PYG">PYG</option>
  304. <option value="QAR">QAR</option>
  305. <option value="RON">RON</option>
  306. <option value="RSD">RSD</option>
  307. <option value="RUB">RUB</option>
  308. <option value="RWF">RWF</option>
  309. <option value="SAR">SAR</option>
  310. <option value="SBD">SBD</option>
  311. <option value="SCR">SCR</option>
  312. <option value="SDG">SDG</option>
  313. <option value="SEK">SEK</option>
  314. <option value="SGD">SGD</option>
  315. <option value="SHP">SHP</option>
  316. <option value="SLL" selected>SLL</option>
  317. <option value="SOS">SOS</option>
  318. <option value="SRD">SRD</option>
  319. <option value="STD">STD</option>
  320. <option value="SYP">SYP</option>
  321. <option value="SZL">SZL</option>
  322. <option value="THB">THB</option>
  323. <option value="TJS">TJS</option>
  324. <option value="TMT">TMT</option>
  325. <option value="TND">TND</option>
  326. <option value="TOP">TOP</option>
  327. <option value="TRY">TRY</option>
  328. <option value="TTD">TTD</option>
  329. <option value="TWD">TWD</option>
  330. <option value="TZS">TZS</option>
  331. <option value="UAH">UAH</option>
  332. <option value="UGX">UGX</option>
  333. <option value="USD">USD</option>
  334. <option value="UYU">UYU</option>
  335. <option value="UZS">UZS</option>
  336. <option value="VEF">VEF</option>
  337. <option value="VND">VND</option>
  338. <option value="VUV">VUV</option>
  339. <option value="WST">WST</option>
  340. <option value="XAF">XAF</option>
  341. <option value="XCD">XCD</option>
  342. <option value="XDR">XDR</option>
  343. <option value="XOF">XOF</option>
  344. <option value="XPF">XPF</option>
  345. <option value="YER">YER</option>
  346. <option value="ZAR">ZAR</option>
  347. <option value="ZMW">ZMW</option>
  348. </select>
  349. </div>
  350. <br style="clear:both;"/>
  351. <br />
  352. <div class="form-group">
  353. <input type="text" class="form-control" id="converted" readonly = "readonly"/>
  354. </div>
  355. <center><button type="button" class="btn btn-primary" id="convert"><span class="glyphicon glyphicon-compressed"></span> Convert</button></center>
  356. </form>
  357. </div>
  358. </div>
  359. </body>
  360. <script src="js/jquery-3.2.1.min.js"></script>
  361. <script src="js/script.js"></script>
  362. </html>

Creating Coverter API

This is where the main function of the application. This code will fetch the converter api from the original site then render it the application. To do this just copy and write these block of codes inside the text editor, then save it as converter.php.

  1. <?php
  2. $amount = $_POST['amount'];
  3. $from = $_POST['frm'];
  4. $to = $_POST['to'];
  5.  
  6.  
  7. $ch = curl_init();
  8.  
  9. curl_setopt($ch, CURLOPT_URL, "<a href="https://free.currencyconverterapi.com/api/v5/convert?q=" rel="nofollow">https://free.currencyconverterapi.com/api/v5/convert?q=</a>{$from}_{$to}&compact=ultra");
  10.  
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  12.  
  13. $output = curl_exec($ch);
  14.  
  15. curl_close($ch);
  16. $data = explode(':', $output);
  17. $data = explode(" ", $data[1]);
  18. $converted = round($data[0], 3);
  19.  
  20. echo number_format($converted * $amount);
  21. ?>

Creating jQuery Script

This is where the code that uses jquery script been used. This code will send the input data via ajax request then will be process in php script to display it back. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. $(document).ready(function(){
  2. $('#convert').on('click', function(){
  3. var amount = $('#amount').val();
  4. var frm = $('#from').val();
  5. var to = $('#to').val();
  6. if(amount == ""){
  7. alert("Please enter some amount first");
  8. }else{
  9. $('#convert').attr('disabled', 'disabled');
  10.  
  11. $.ajax({
  12. url: 'converter.php',
  13. type: 'POST',
  14. data:{
  15. amount: amount,
  16. frm: frm,
  17. to: to
  18. },
  19. success:function(data){
  20. $('#converted').val('');
  21. setTimeout(function(){
  22. $('#convert').removeAttr('disabled');
  23. $('#converted').val(data);
  24. }, 2000);
  25. }
  26. });
  27. }
  28. });
  29. });

There you have it we successfully created a Convert Currency With Currency API. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.