| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- /**
- * @author:王刚涛
- * 用于获取历史数据各种指数
- * http://hq.sinajs.cn/list=s_sz000002
- */
- class Input_163
- {
- const GLOBAL_URL_0 = "http://quotes.money.163.com/service/chddata.html?code=";
- const GLOBAL_URL_1 = "&start=";
- const GLOBAL_URL_2 = "&end=";
- const GLOBAL_URL_3 = "&fields=TOPEN;HIGH;LOW;TCLOSE;VATURNOVER;VOTURNOVER;CHG;PCHG";
- static private function curl_get_data($url){
- $curlHandle = curl_init();
- curl_setopt($curlHandle, CURLOPT_URL, $url);
- curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
- $content = curl_exec($curlHandle);
- curl_close($curlHandle);
- $content1 = explode("\n", $content);//以换行符进行分割字符串
- return $content1;
- }
- //接口使用的编码,深圳加1,上海加0
- static private function getCodeForURL($code)
- {
- $preNum = '0';
- $index = Index::getInfoByCode($code);
- if ($index['exchange'] == STOCK_EXCHANGE_SZ) {
- $preNum = '1';
- }
- return $preNum.$code;
- }
- //深市返回的成交量以个为单位,需转换为以手为单位
- static private function getAmountForIndexDayK($code, $amount)
- {
- $newAmount = $amount;
- $index = Index::getInfoByCode($code);
- if ($index['exchange'] == STOCK_EXCHANGE_SZ) {
- $newAmount = round($amount / 100);
- }
- return $newAmount;
- }
- //获取指定时间段指数的历史数据
- static private function getPartIndexDayK($code, $start_date, $end_date)
- {
- $urlCode = self::getCodeForURL($code);
- $url = self::GLOBAL_URL_0.$urlCode.self::GLOBAL_URL_1.$start_date.self::GLOBAL_URL_2.$end_date.self::GLOBAL_URL_3;
- $contentList = self::curl_get_data($url);
- if (empty($contentList[1])) { //只有标题,没有内容
- return;
- }
- for ($i = count($contentList) - 1; $i > 0; $i--) { //倒叙遍历,日期是倒叙的,不遍历标题
- $content = trim($contentList[$i]);
- if (empty($content)) {
- continue;
- }
- $item = explode(",", iconv("gbk", "utf-8", $content));
- $data['code'] = explode("'",$item[1])[1];
- $data['date'] = ConverseDate($item[0]);
- $data['name'] = trim($item[2]);
- $data['timestamp'] = strtotime($item[0]); //当日零点的时间戳
- $data['open_price'] = floatval(EmptyToZero($item[3]));
- $data['highest_price'] = floatval(EmptyToZero($item[4]));
- $data['lowest_price'] = floatval(EmptyToZero($item[5]));
- $data['close_price'] = floatval(EmptyToZero($item[6]));
- $data['value'] = floatval($item[7]);
- $data['amount'] = self::getAmountForIndexDayK($data['code'],floatval($item[8]));
- $data['increase_price'] = floatval(EmptyToZero($item[9]));
- $data['increase_ratio'] = floatval(EmptyToZero($item[10]));
- if ($data['close_price'] == 0) { //当收盘价为0的时候,表示此时的基于9-15点,所以不存储
- continue;
- }
- if (!empty($data['name'])) { //判断非法股票代码
- Index_day_k::addOrUpdateByCodeDate($data);
- }
- }
- return;
- }
- //获取自20140101到前一天的指定的指数数据
- static public function getIndexDayK()
- {
- $indexList = Index::getAllList();
- $end_date = (int)date('Ymd', time());
- $end_year = (int)($end_date / 10000);
- for($year = GLOBAL_BEGIN_YEAR; $year < $end_year; $year++) {
- foreach($indexList as $item) {
- self::getPartIndexDayK($item['code'], $year.'0101', $year.'1231');
- }
- }
- foreach($indexList as $item) {
- self::getPartIndexDayK($item['code'], $end_year.'0101', $end_year.'1231');
- }
- return;
- }
- }
- ?>
|