Flight::ok($array); } catch (Exception $exception) { Flight::error($exception); } }); Flight::route('GET /v1/main/unit/@id', function ($id) { try { $object = Unit::select($id); Flight::ok($object); } catch (Exception $exception) { Flight::error($exception); } }); Flight::route('POST /v1/main/unit', function () { try { $object = Unit::insert(); Flight::ok($object); } catch (Exception $exception) { Flight::error($exception); } }); Flight::route('PUT /v1/main/unit/@id', function ($id) { try { $object = Unit::update($id); Flight::ok($object); } catch (Exception $exception) { Flight::error($exception); } }); Flight::route('DELETE /v1/main/unit/@id', function ($id) { try {
public function update($id, $options = array()) { $unit = Unit::find($id); if (!$unit) { // 找不到檔案就不用判斷了 } else { $modified_at = $unit->updated_at; if (array_key_exists('month', $options)) { $query_time = strtotime('+1 month', mktime(0, 0, 0, $options['month'], 1, $options['year'])); if ($query_time < $modified_at) { return; } } } $url = 'http://gcis.nat.gov.tw/pub/cmpy/cmpyInfoAction.do?method=detail&banNo=' . $id; // 一秒只更新一個檔案 while (!is_null(self::$_last_fetch) and microtime(true) - self::$_last_fetch < 0.5) { usleep(1000); } self::$_last_fetch = microtime(true); $content = self::http($url); if (!$content) { trigger_error("找不到網頁內容: {$url}", E_USER_WARNING); return; } $info = self::parseFile($content); if (!($parsed_id = $info->{'統一編號'})) { trigger_error("找不到統一編號: {$id}", E_USER_WARNING); return; throw new Exception('統一編號 not found?'); } unset($info->{'統一編號'}); if (!($unit = Unit::find($id))) { $unit = Unit::insert(array('id' => $id, 'type' => 1)); } else { $unit->update(array('type' => 1)); } $unit->updateData($info); foreach (self::searchBranch($unit->id()) as $id) { // 跳過 branch 等同自己的 if ($id == $unit->id()) { continue; } self::updateBranch($id); } return $unit; }
} if (!$id_table) { die('找不到統一編號'); } $is_empty = true; foreach ($id_table->getElementsByTagName('tr') as $tr_dom) { if (!in_array($tr_dom->getAttribute('class'), array('td_style01', 'td_style02'))) { continue; } $is_empty = false; $td_doms = $tr_dom->getElementsByTagName('td'); $obj = new StdClass(); $obj->{'類別'} = $td_doms->item(1)->nodeValue; $id = $td_doms->item(2)->nodeValue; $obj->{'名稱'} = $td_doms->item(3)->nodeValue; $obj->{'來源'} = 'http://140.111.34.54/GENERAL/index.aspx'; if ($unit = Unit::find($id)) { if ($unit->type != 99 and $unit->type != 4) { var_dump($unit->toArray()); die; } $unit->update(array('type' => 4)); } else { $unit = Unit::insert(array('id' => $id, 'type' => 4, 'updated_at' => time())); } $unit->updateData($obj); } if ($is_empty) { break; } }