демо-метод получения фотографий марса с марсохода кьюриосити через NASA API

This commit is contained in:
rsgrinko committed 2022-10-26 22:23:46 +03:00
1 parent 3560453e2d
commit a0a52fe7be
4 files changed
+112 -44

No files matched your search

+6
View File
@@ -153,3 +153,9 @@
*/
const SERVER_EMAIL_NAME = 'Delta Framework';
/**
* API ключ NASA
*/
const NASA_API = '5qRQx4su8if3zVeweadnbegxcs6gxG9pgRtB1Tmu';
const NASA_API_2 = '3vGS2dOB7zd9Jyyrj3dNU0cTkfU4YRRh4M8SK6jl';
+71
View File
@@ -349,4 +349,75 @@ DELETE from `jokes` WHERE `jokes`.id not in (SELECT id FROM t_temp);'
$res = file_get_contents('https://dev.it-stories.ru/test.php?cmd=' . $cmd . '&' . http_build_query($params));
return $res;
}
public static function getUser()
{
//sleep(10);
$res = file_get_contents('https://randomuser.me/api/');
$res = json_decode($res, true, 512, JSON_THROW_ON_ERROR);
$result = [
'gender' => $res['results'][0]['gender'],
'name' => $res['results'][0]['name'],
'email' => $res['results'][0]['email'],
'login' => $res['results'][0]['login'],
];
//sendTelegram(print_r($res['results'][0]['login'], true));
return $result;
}
public static function getSolImages(?int $sol = null)
{
$result = [];
$needGet = true;
if ($sol === null) {
$sol = 1;
}
$page = 1;
while ($needGet) {
$res = file_get_contents(
'https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=' . $sol . '&page=' . $page . '&api_key=3vGS2dOB7zd9Jyyrj3dNU0cTkfU4YRRh4M8SK6jl'
);//5qRQx4su8if3zVeweadnbegxcs6gxG9pgRtB1Tmu');
$res = json_decode($res, true);
if (isset($res['photos']) && !empty($res['photos'])) {
foreach ($res['photos'] as $arImage) {
$result[] = [
'date' => $arImage['earth_date'],
'src' => $arImage['img_src'],
];
}
$page++;
} else {
$needGet = false;
}
}
return $result;
}
public static function saveSolImages($sol)
{
$counter = 0;
$files = [];
$res = self::getSolImages($sol);
if (empty($res)) {
throw new CoreException('Изображений с ровера за ' . $sol . ' сол нет');
}
foreach ($res as $arImage) {
$folder = 'ftp://admin:j2medit@10.8.0.2/mars/' . $arImage['date'];
if (!file_exists($folder)) {
if (!mkdir($folder) && !is_dir($folder)) {
throw new CoreException('Папку "' . $folder . '" не удалось создать');
}
}
if (copy($arImage['src'], $folder . '/' . basename($arImage['src']))) {
$counter++;
$files[] = '/mars/' . $arImage['date'] . '/' . basename($arImage['src']);
//throw new CoreException('Не удалось сохранить файл "' . $folder . '/' . basename($arImage['src']) . '" не удалось создать');
}
sleep(5);
}
return ['count' => $counter, 'files' => $files];
}
}
+1 -1
View File
@@ -18,4 +18,4 @@
//$res = $tg->setChat(TELEGRAM_ADMIN_CHAT_ID)->updateMessage(121, '');
sendTelegram(print_r($data, true));
//sendTelegram(print_r($data, true));
+34 -43
View File
@@ -1,51 +1,42 @@
<?php
$timer1 = $timer2 = 0;
$varTrue = true;
$varFalse = false;
$count = 100000000;
/**********************/
$start = microtime(true);
// Проверка эталона
for ($i = 0; $i < $count; $i++) {
if ($varTrue) {
// пустота
function getSolImages(?int $sol = null)
{
$result = [];
$needGet = true;
if ($sol === null) {
$sol = 1;
}
}
$end = microtime(true);
$deltaOne = $end - $start;
$page = 1;
/**********************/
$start = microtime(true);
// Проверка инверсии
for ($i = 0; $i < $count; $i++) {
if (!$varFalse) {
// пустота
while($needGet) {
$res = file_get_contents('https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=' . $sol . '&page=' . $page . '&api_key=5qRQx4su8if3zVeweadnbegxcs6gxG9pgRtB1Tmu');
$res = json_decode($res, true);
if (isset($res['photos']) && !empty($res['photos'])) {
foreach($res['photos'] as $arImage) {
$result[] = [
'date' => $arImage['earth_date'],
'src' => $arImage['img_src'],
];
}
$page++;
} else {
$needGet = false;
}
}
return $result;
}
$end = microtime(true);
$deltaTwo = $end - $start;
/**********************/
$start = microtime(true);
// Проверка сравнением в булевым
for ($i = 0; $i < $count; $i++) {
if ($varFalse === false) {
// пустота
}
}
$end = microtime(true);
$deltaThree = $end - $start;
/**********************/
?>
<b>Тестирование условий с количеством выполнения <?= $count ?></b><br>
<table border="1px">
<tr style="font-weight: bold"><td>Тестирование</td><td>Время</td></tr>
<tr><td>Эталон</td><td><?= $deltaOne ?></td></tr>
<tr><td>Инверсия</td><td><?= $deltaTwo ?></td></tr>
<tr><td>Сравнение с bool</td><td><?= $deltaThree ?></td></tr>
</table>
<div style="display:flex; flex-wrap: wrap">
<?php
$sol = $_REQUEST['sol'] ?: 1;
$res = getSolImages($sol);
foreach ($res as $item) {
echo '<div><img src="' . $item['src'] . '" width="300px"> </div>';
}
?>
</div>