修改了corn.php,使其能够获取nasa的每日一图

This commit is contained in:
zhangdawei 2023-12-05 18:24:03 +08:00 committed by 六零
parent 03a80ae8bf
commit bdabc03cd2
7 changed files with 210 additions and 114 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

9
.idea/lylme_spage.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/lylme_spage.iml" filepath="$PROJECT_DIR$/.idea/lylme_spage.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -1,123 +1,59 @@
<?php <?php
header("Content-Type: text/html; charset=utf-8"); header("Content-Type: text/html; charset=utf-8");
$pass = ""; //在这里配置密钥 $pass = ""; // 在这里配置密钥
/* //GET https://域名/assets/img/cron.php?key=填你的密钥
为保证安全,已禁止空密钥执行,请在 $pass 的引号内添加密钥
CRON任务GET http://域名/assets/img/cron.php?key=设置的密钥
例如: if (empty($pass)) {
$pass = 'lylme';
CRON地址为http://hao.lylme.com/assets/img/cron.php?key=lylme
*/
//########以下内容可忽略########
if(empty($pass)){
//密钥为空
exit('错误禁止空密钥执行CRON请在cron.php文件配置密钥'); exit('错误禁止空密钥执行CRON请在cron.php文件配置密钥');
} } else if (empty($_GET['key'])) {
else if (empty($_GET['key'])) {
//未传入key
exit('错误密钥为空请传入包含参数key的GET请求<br> exit('错误密钥为空请传入包含参数key的GET请求<br>
请求示例:<b>http://'.$_SERVER['HTTP_HOST'].'/assets/img/cron.php?key=秘钥</b>'); 请求示例:<b>https://域名/assets/img/cron.php?key=adsij' . $_SERVER['HTTP_HOST'] . '/assets/img/cron.php?key=秘钥</b>');
} } else if ($pass != $_GET['key']) {
else if($pass != $_GET['key']){
//密钥错误
exit('错误传入参数key与密钥不匹配'); exit('错误传入参数key与密钥不匹配');
} } else {
else { // 密钥正确,执行下面代码
//密钥正确,执行下面代码
/** /**
* PHP获取bing每日壁纸 * PHP下载NASA APOD每日高清图片并保存为background.jpg
* bing每日壁纸更新时间为UTC+8 1600
*/ */
if($_GET['idx']==null){ $api_key = ''; // 替换成你的NASA API密钥
$str=file_get_contents('http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1'); $apod_url = 'https://api.nasa.gov/planetary/apod?api_key=' . $api_key;
// $json_content = file_get_contents($apod_url);
$data = json_decode($json_content);
} if ($data && isset($data->hdurl)) {
else{ $hdurl = $data->hdurl;
$str=file_get_contents('http://cn.bing.com/HPImageArchive.aspx?idx='.$_GET['idx'].'&n=1');
}
if(preg_match("/<url>(.+?)<\/url>/is",$str,$matches)){ echo "高清图片地址:" . $hdurl . "<br>";
if(preg_match("/<fullstartdate>(.+?)<\/fullstartdate>/is",$str,$cdata)){ /**
* 下载高清图片并保存为background.jpg
}
$crdate = date('Y年m月d日 H:i', strtotime($cdata[1]));
$imgurl = 'http://cn.bing.com'.$matches[1];
echo "壁纸地址:" . $imgurl."<br>";
echo "发布时间:" . $crdate."<br>";
}
/**
* 将bing每日壁纸保存到 当前目录/background.jpg
*/ */
function GrabImage($imgurl, $dir, $filename='/background.jpg'){ function DownloadAndSaveImage($imgurl, $dir, $filename = '/background.jpg')
if(empty($imgurl)){ {
return false; if (empty($imgurl)) {
}
$ext = strrchr($imgurl, '.');
if($ext == '.js' && $ext == ".html" && $ext == ".php"){
echo "Format not supported";
return false; return false;
} }
$dir = realpath($dir); $dir = realpath($dir);
$filename = $dir . $filename; $filename = $dir . $filename;
ob_start(); $img_content = file_get_contents($imgurl);
readfile($imgurl);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2 = fopen($filename , "w");
if(fwrite($fp2, $img)==true){
echo "壁纸大小:" . round($size / 1024) .'KB<br>';
echo "<p><font color='green'>成功当前壁纸已与Bing同步</font></p>";
if ($img_content !== false) {
file_put_contents($filename, $img_content);
echo "成功:高清图片已下载并保存为 " . $filename . "<br>";
} else {
echo "<p><font color='red'>错误:下载图片失败</font></p>";
} }
else{
echo "<p><font color='red'>错误: 保存文件 <b>". $filename."</b> 失败,请检查目录权限</font></p>";
} }
fclose($fp2);
// echo "保存路径:" . $filename."<br>"; DownloadAndSaveImage($hdurl, dirname(__FILE__));
} else {
return $filename; echo "<p><font color='red'>错误:未能获取到高清图片地址</font></p>";
} }
GrabImage($imgurl,dirname(__FILE__));
/**
* 返回bing每日壁纸
*/
// if($imgurl){
// header('Content-Type: image/JPEG');
// @ob_end_clean();
// @readfile($imgurl);
// @flush(); @ob_flush();
// exit();
// }else{
// exit('error');
// }
} }
?> ?>

123
assets/img/cron_old.php Normal file
View File

@ -0,0 +1,123 @@
<?php
header("Content-Type: text/html; charset=utf-8");
$pass = ""; //在这里配置密钥
/*
为保证安全,已禁止空密钥执行,请在 $pass 的引号内添加密钥
CRON任务GET http://域名/assets/img/cron.php?key=设置的密钥
例如:
$pass = 'lylme';
CRON地址为http://hao.lylme.com/assets/img/cron.php?key=lylme
*/
//########以下内容可忽略########
if(empty($pass)){
//密钥为空
exit('错误禁止空密钥执行CRON请在cron.php文件配置密钥');
}
else if (empty($_GET['key'])) {
//未传入key
exit('错误密钥为空请传入包含参数key的GET请求<br>
请求示例:<b>http://'.$_SERVER['HTTP_HOST'].'/assets/img/cron.php?key=秘钥</b>');
}
else if($pass != $_GET['key']){
//密钥错误
exit('错误传入参数key与密钥不匹配');
}
else {
//密钥正确,执行下面代码
/**
* PHP获取bing每日壁纸
* bing每日壁纸更新时间为UTC+8 1600
*/
if($_GET['idx']==null){
$str=file_get_contents('http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1');
//
}
else{
$str=file_get_contents('http://cn.bing.com/HPImageArchive.aspx?idx='.$_GET['idx'].'&n=1');
}
if(preg_match("/<url>(.+?)<\/url>/is",$str,$matches)){
if(preg_match("/<fullstartdate>(.+?)<\/fullstartdate>/is",$str,$cdata)){
}
$crdate = date('Y年m月d日 H:i', strtotime($cdata[1]));
$imgurl = 'http://cn.bing.com'.$matches[1];
echo "壁纸地址:" . $imgurl."<br>";
echo "发布时间:" . $crdate."<br>";
}
/**
* 将bing每日壁纸保存到 当前目录/background.jpg
*/
function GrabImage($imgurl, $dir, $filename='/background.jpg'){
if(empty($imgurl)){
return false;
}
$ext = strrchr($imgurl, '.');
if($ext == '.js' && $ext == ".html" && $ext == ".php"){
echo "Format not supported";
return false;
}
$dir = realpath($dir);
$filename = $dir . $filename;
ob_start();
readfile($imgurl);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2 = fopen($filename , "w");
if(fwrite($fp2, $img)==true){
echo "壁纸大小:" . round($size / 1024) .'KB<br>';
echo "<p><font color='green'>成功当前壁纸已与Bing同步</font></p>";
}
else{
echo "<p><font color='red'>错误: 保存文件 <b>". $filename."</b> 失败,请检查目录权限</font></p>";
}
fclose($fp2);
// echo "保存路径:" . $filename."<br>";
return $filename;
}
GrabImage($imgurl,dirname(__FILE__));
/**
* 返回bing每日壁纸
*/
// if($imgurl){
// header('Content-Type: image/JPEG');
// @ob_end_clean();
// @readfile($imgurl);
// @flush(); @ob_flush();
// exit();
// }else{
// exit('error');
// }
}
?>