Last active
December 14, 2015 03:19
-
-
Save nowrep/5020043 to your computer and use it in GitHub Desktop.
Extract currency rates from moneypolo.cz
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
| <?php | |
| class MoneyPoloRates | |
| { | |
| public $EUR; | |
| public $GBP; | |
| public $USD; | |
| private $reply; | |
| private $error = true; | |
| private $errorMessage; | |
| public function MoneyPoloRates() | |
| { | |
| $this->getAjaxContents(); | |
| $this->extractRates(); | |
| } | |
| public function error() | |
| { | |
| return $this->error; | |
| } | |
| public function errorMessage() | |
| { | |
| return $this->errorMessage == "" ? "Unknown error" : $this->errorMessage; | |
| } | |
| private function getAjaxContents() | |
| { | |
| // Maybe use some cache on disk? | |
| $url = "http://moneypolo.cz/wp-content/themes/moneypolo/ajax-handler.php"; | |
| $data = "func=getCurrencyRates"; | |
| try { | |
| $this->reply = $this->do_post_request($url, $data); | |
| } | |
| catch (Exception $e) { | |
| $this->error = true; | |
| $this->errorMessage = $e->getMessage(); | |
| } | |
| } | |
| private function extractRates() | |
| { | |
| // $this->reply is in JSON format | |
| $decoded = json_decode($this->reply); | |
| if ($decoded === null) { | |
| $this->error = true; | |
| $this->errorMessage = "Cannot decode JSON!"; | |
| return; | |
| } | |
| if ($decoded->{"success"} === false) { | |
| $this->error = true; | |
| $this->errorMessage = "Remote server returned success = fail."; | |
| return; | |
| } | |
| $data = $decoded->{"data"}; | |
| $dataEUR = $data->{"EUR"}; | |
| $this->EUR["buy"] = $dataEUR->{"MTCZKBUY"}->{"Rate"}; | |
| $this->EUR["sell"] = $dataEUR->{"MTCZKSELL"}->{"Rate"}; | |
| $dataGBP = $data->{"GBP"}; | |
| $this->GBP["buy"] = $dataGBP->{"MTCZKBUY"}->{"Rate"}; | |
| $this->GBP["sell"] = $dataGBP->{"MTCZKSELL"}->{"Rate"}; | |
| $dataUSD = $data->{"USD"}; | |
| $this->USD["buy"] = $dataUSD->{"MTCZKBUY"}->{"Rate"}; | |
| $this->USD["sell"] = $dataUSD->{"MTCZKSELL"}->{"Rate"}; | |
| $this->error = false; | |
| $this->errorMessage = "No error"; | |
| } | |
| // http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/ | |
| private function do_post_request($url, $data, $optional_headers = null) | |
| { | |
| $params = array('http' => array( | |
| 'method' => 'POST', | |
| 'content' => $data | |
| )); | |
| if ($optional_headers!== null) { | |
| $params['http']['header'] = $optional_headers; | |
| } | |
| $ctx = stream_context_create($params); | |
| $fp = @fopen($url, 'rb', false, $ctx); | |
| if (!$fp) { | |
| throw new Exception("Problem with $url, $php_errormsg"); | |
| } | |
| $response = @stream_get_contents($fp); | |
| if ($response === false) { | |
| throw new Exception("Problem reading data from $url, $php_errormsg"); | |
| } | |
| return $response; | |
| } | |
| } | |
| // Usage | |
| $c = new MoneyPoloRates(); | |
| if ($c->error()) { | |
| echo "<b>Error:</b> " . $c->errorMessage() . "<br/>"; | |
| } | |
| else { | |
| echo "Currency - Buy - Sell<br/>"; | |
| echo "EUR: " . $c->EUR["buy"] . " / " . $c->EUR["sell"] . "<br/>"; | |
| echo "GBP: " . $c->GBP["buy"] . " / " . $c->GBP["sell"] . "<br/>"; | |
| echo "USD: " . $c->USD["buy"] . " / " . $c->USD["sell"] . "<br/>"; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment