Exemple #1
0
 /**
  * 获取汇率
  * 雅虎YQL https://developer.yahoo.com/yql/console/
  * 请求语句 select * from yahoo.finance.xchange where pair="CNYUSD,CNYHKD"
  * 示例 https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%3D%22CNYUSD%2CCNYHKD%22%0A%09%09&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=
  */
 public function actionExrates()
 {
     $units = tools::getUnits();
     unset($units['CNY']);
     $arr = array();
     foreach ($units as $k => $v) {
         $arr[] = 'CNY' . $k;
     }
     $str = join(',', $arr);
     $url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%3D%22{$str}%22%0A%09%09&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=";
     $output = zmf::curlget($url);
     if (!$output) {
         exit('Failed');
     }
     $rateArr = CJSON::decode($output);
     $rateArr = $rateArr['query']['results']['rate'];
     foreach ($rateArr as $val) {
         $_key = str_replace('CNY', '', $val['id']);
         $rates[$_key] = array('rate' => $val['Rate'], 'title' => tools::getUnits($_key));
     }
     $detailDir = Yii::app()->basePath . '/runtime/rates/';
     zmf::createUploadDir($detailDir);
     $dir = $detailDir . 'detail.log';
     file_put_contents($dir, CJSON::encode($rates));
     exit('well done');
 }