/** * Creates a new Stock model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Stock(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->idStock]); } else { return $this->render('create', ['model' => $model]); } }
/** * 同步当前股票信息 */ public function actionSyncStock() { $sleep = 5; while (true) { try { $hourm = intval(date('Hm')); if ($hourm > 900 && $hourm < 1135 || $hourm > 1300 && $hourm < 1510) { $pici = time(); $url_arr = ['http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=1&num=10000&sort=changepercent&asc=0&node=sz_a&symbol=&_s_r_a=page&time=' . time(), 'http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=1&num=10000&sort=changepercent&asc=0&node=sh_a&symbol=&_s_r_a=page']; foreach ($url_arr as $url) { $return_arr = Common::reqUrl($url, [], true, 30, true); if ($return_arr['content']) { $arr = Common::ext_json_decode(mb_convert_encoding($return_arr['content'], "utf-8", "gbk"), true); if ($arr) { foreach ($arr as $row) { $Stock = Stock::find()->where(['symbol' => $row['symbol']])->one(); if ($Stock) { $Stock->scenario = 'update'; } else { $Stock = new Stock(['scenario' => 'create']); $Stock->updated_at = time(); } $save_arr = []; $save_arr['Stock'] = $row; if ($Stock->id && $row['trade'] != $Stock->trade) { $StockLog = new StockLog(['scenario' => 'create']); $StockLog->stock_id = $Stock->id; $StockLog->pici = $pici; $StockLog->setAttributes($Stock->attributes); $StockLog->id = ''; $StockLog->save(); } if ($Stock->load($save_arr) && $Stock->save()) { } } } } } } sleep($sleep); } catch (Exception $e) { } } }