Last active
April 12, 2018 05:28
-
-
Save ankitraturi/1796975931eb9387a89bffb8ec31ffd0 to your computer and use it in GitHub Desktop.
XE Live currency conversion in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function convertCurrency($from = null, $to = null, $amt = null) { | |
| $url = "https://www.xe.com/currencyconverter/convert/?Amount=$amt&From=$from&To=$to"; | |
| $ch = curl_init(); // Initialize a CURL session. | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return Page contents. | |
| curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter. | |
| $data = curl_exec($ch); // grab URL and pass it to the variable. | |
| curl_close($ch); // close curl resource, and free up system resources. | |
| $dom = new DOMDocument(); | |
| @$dom->loadHTML($data); | |
| $xpath = new DOMXPath($dom); | |
| $xpath_resultset = $xpath->query("//span[@class='uccResultAmount']"); | |
| $htmlString = $dom->saveHTML($xpath_resultset->item(0)); | |
| $htmlString = preg_replace("/[^0-9.]/", "", $htmlString); | |
| $converted = (float) $htmlString; | |
| return round($converted, 2); | |
| } | |
| //convertCurrency('USD', 'INR', 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment