Ejemplo n.º 1
0
 /**
  * Api function for crawl golds.
  *
  * @return Response
  */
 public function getGoldDaily()
 {
     $apies = json_decode(EXR_GOLDS);
     // Crawl bank api url.
     foreach ($apies as $type => $url) {
         switch ($type) {
             case 'SJC':
                 $content = file_get_contents($url);
                 if (!$content) {
                     return false;
                 }
                 $doc = new \DOMDocument();
                 @$doc->loadXML($content);
                 $xpath = new \DOMXpath($doc);
                 $items = $xpath->query('//city');
                 $codes = array();
                 foreach ($items as $item) {
                     $codes[] = $item->getAttribute('name');
                 }
                 // Get currency by ids
                 $cities = Currency::where('type', 'GOLD')->get();
                 // Get exchange rates over bank api.
                 foreach ($cities as $city) {
                     $codes = explode('|', $city->code);
                     $item = $xpath->query('//city[@name="' . $codes[0] . '"]/item[@type="' . $codes[1] . '"]')->item(0);
                     if ($item) {
                         $currencyId = $city->id;
                         $buy = $item->getAttribute('buy');
                         $sell = $item->getAttribute('sell');
                         CurrencyDaily::updateGoldsDaily($currencyId, $buy, $sell, 'gold');
                     }
                 }
                 break;
         }
     }
     return Response::json(array('status' => 'Success', 'data' => $apies), 200);
 }