示例#1
0
 /**
  * Api function for crawl bank daily data.
  *
  * @return Response
  */
 public function getBankDaily()
 {
     $apies = json_decode(EXR_BANKS);
     // Crawl bank api url.
     foreach ($apies as $type => $url) {
         switch ($type) {
             case 'VCB':
                 $content = file_get_contents($url);
                 if (!$content) {
                     return false;
                 }
                 $doc = new \Domdocument();
                 @$doc->loadxml($content);
                 $xpath = new \Domxpath($doc);
                 $items = $xpath->query('//Exrate');
                 $codes = array();
                 foreach ($items as $item) {
                     $codes[] = $item->getattribute('CurrencyCode');
                 }
                 // get currency by ids
                 $currencies = Currency::getCurrenciesByCodes($codes, $type);
                 // get exchange rates over bank api.
                 if (count($currencies)) {
                     foreach ($currencies as $currency) {
                         $item = $xpath->query('//Exrate[@CurrencyCode="' . $currency->code . '"]')->item(0);
                         $currencyid = $currency->id;
                         $buy = $item->getattribute('Buy');
                         $transfer = $item->getattribute('Transfer');
                         $sell = $item->getattribute('Sell');
                         if ($buy && $transfer && $sell) {
                             CurrencyDaily::updateExratesDaily($currencyid, $buy, $transfer, $sell, 'bank');
                         }
                     }
                 }
                 break;
         }
     }
     return Response::json(array('status' => 'Success', 'data' => $apies), 200);
 }