kokjan/console/controllers/ConsoleController.php

165 lines
6.4 KiB
PHP

<?php
namespace console\controllers;
use common\models\CmsEgp;
use yii\base\Exception;
use yii\console\Controller;
class ConsoleController extends Controller
{
/*
public function actionEgp()
{
$departmentId = '6342208';
$type = [
'P0' => 'แผนการจัดซื้อจัดจ้าง',
'15' => 'ประกาศราคากลาง',
'B0' => 'ร่างเอกสารประกวดราคา',
'D0' => 'ประกาศเชิญชวน',
'W0' => 'ประกาศรายชื่อผู้ชนะการเสนอราคา',
'D1' => 'ยกเลิกประกาศเชิญชวน',
'W1' => 'ยกเลิกประกาศรายชื่อผู้ชนะการเสนอราคา',
'D2' => 'เปลียนแปลงประกาศเชิญชวน',
'W2' => 'เปลี่ยนแปลงประกาศรายชื่อ'
];
foreach ($type as $key => $value) {
$url = 'http://process3.gprocurement.go.th/EPROCRssFeedWeb/egpannouncerss.xml?deptId=' . $departmentId . '&anounceType=' . $key;
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_ENCODING => 'UTF-8'
]);
$data = curl_exec($curl);
curl_close($curl);
$xml = @simplexml_load_string($data);
//$xml = simplexml_load_file($url);
if ($xml) {
foreach ($xml->channel->item as $item) {
//$returnString .= '<li><a href="' . $item->link . '" target="_blank">' . $item->title . '</a> <small>เมื่อ ' . Yii::$app->formatter->asDate(strtotime(substr($item->pubDate, 0, 16))) . '</small></li>';
//var_dump($item);
$url_components = parse_url($item->link);
parse_str($url_components['query'], $params);
$model_check = CmsEgp::find()->where(['link' => $item->link, 'type' => $key])->orderBy(['pub_date' => SORT_DESC])->one();
if (!isset($model_check)) { // || ($model_check->pub_date <= $item->pubDate)
$model = new CmsEgp();
$model->type = (string) $key;
//$model->project_id = (string) $params['projectId'];
$model->name = (string) $item->title;
$model->link = (string) $item->link;
$model->pub_date = $item->pubDate;
try {
if ($model->save()) {
//echo 'Save: ' . $model->name;
//error_log('Save: ' . $model->name);
} else {
//echo $model->getFirstErrors();
//error_log($model->getFirstErrors());
}
} catch (Exception $exception) {
return $exception->getMessage();
}
}
}
}
}
}*/
public function actionEgp()
{
$departmentId = '6342208';
$type = [
'P0' => 'แผนการจัดซื้อจัดจ้าง',
'15' => 'ประกาศราคากลาง',
'B0' => 'ร่างเอกสารประกวดราคา',
'D0' => 'ประกาศเชิญชวน',
'W0' => 'ประกาศรายชื่อผู้ชนะการเสนอราคา',
'D1' => 'ยกเลิกประกาศเชิญชวน',
'W1' => 'ยกเลิกประกาศรายชื่อผู้ชนะการเสนอราคา',
'D2' => 'เปลี่ยนแปลงประกาศเชิญชวน',
'W2' => 'เปลี่ยนแปลงประกาศรายชื่อ'
];
foreach ($type as $key => $value) {
$url = 'https://process.gprocurement.go.th/EPROCRssFeedWeb/egpannouncerss.xml?deptId=' . $departmentId . '&anounceType=' . $key;
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
CURLOPT_TIMEOUT => 300,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
]);
$data = curl_exec($curl);
if (curl_errno($curl)) {
curl_close($curl);
continue;
}
curl_close($curl);
if (empty($data)) {
continue;
}
libxml_use_internal_errors(true);
$xml = simplexml_load_string($data);
if ($xml === false) {
libxml_clear_errors();
continue;
}
if (!isset($xml->channel->item)) {
continue;
}
foreach ($xml->channel->item as $item) {
$url_components = parse_url($item->link);
parse_str($url_components['query'] ?? '', $params);
$model_check = CmsEgp::find()
->where(['link' => (string)$item->link, 'type' => $key])
->orderBy(['pub_date' => SORT_DESC])
->one();
if (!isset($model_check)) {
$model = new CmsEgp();
$model->type = (string) $key;
$model->name = (string) $item->title;
$model->link = (string) $item->link;
$model->pub_date = (string) $item->pubDate;
try {
$model->save();
} catch (Exception $exception) {
// Logging or error handling (ถ้าจำเป็น)
}
}
}
}
}
}