Skip to content

Commit 1700502

Browse files
author
YieldRay
committed
support php5.3
1 parent d1590be commit 1700502

6 files changed

Lines changed: 23 additions & 16 deletions

File tree

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@
2424

2525
fork 后,修改自己仓库的 `url.csv`,然后在 Vercel 平台上导入自己的项目
2626
你也可以直接修改<https://github.com/YieldRay/Random-Picture/blob/master/url.csv>来创建 fork
27-
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/git?s=https%3A%2F%2Fgithub.com%2FYieldRay%2FRandom-Picture)
28-
php 版本也可以直接上传到虚拟主机
27+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/git?s=https%3A%2F%2Fgithub.com%2FYieldRay%2FRandom-Picture)
28+
29+
## php 部署到虚拟主机
30+
31+
支持 php >= 5.3
32+
直接将下载项目然后上传至虚拟主机即可,此时 API 路径在 `./api` 文件夹下
33+
或者下载项目的 `./api/index.php``./url.csv` ,将这两个文件上传至同一目录即可
2934

3035
## deno 部署到 deno.dev
3136

api/index.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<?php
2-
32
const ALLOW_RAW_OUTPUT = false;
43
// 是否开启 ?raw 选项,可能会消耗服务器较多流量
54

65
function has_query($query)
76
{
87
return isset($_GET[$query]);
98
}
10-
if (file_exists('../url.csv')) {
9+
10+
if (file_exists(__DIR__ . '/url.csv')) // in the same folder
11+
$imgs_array = file(__DIR__ . '/url.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
12+
else if (file_exists('../url.csv')) // in the parent folder
1113
$imgs_array = file('../url.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
12-
} else {
14+
else // for vercel runtime
1315
$imgs_array = file('http://' . $_SERVER['HTTP_HOST'] . '/url.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
14-
}
15-
if (count($imgs_array) == 0) {
16-
$imgs_array = ['https://http.cat/503'];
17-
}
16+
if (count($imgs_array) == 0) $imgs_array = array('https://http.cat/503');
17+
1818

19-
$id = $_GET['id'] ?? "";
19+
$id = $_GET['id'] ? $_GET['id'] : "";
2020
if (strlen($id) > 0 && is_numeric($id)) {
2121
settype($id, 'int');
2222
$len = count($imgs_array);
@@ -34,7 +34,7 @@ function has_query($query)
3434
if (has_query('json')) {
3535
header('Access-Control-Allow-Origin: *');
3636
header('Content-Type: application/json');
37-
echo json_encode(['id' => $id, 'url' => $imgs_array[$id]]);
37+
echo json_encode(array('id' => $id, 'url' => $imgs_array[$id]));
3838
} else if (has_query('raw')) {
3939
if (!ALLOW_RAW_OUTPUT) {
4040
header('HTTP/1.1 403 Forbidden');
@@ -43,6 +43,7 @@ function has_query($query)
4343
header('Content-Type: image/png');
4444
echo file_get_contents($imgs_array[$id]);
4545
} else {
46+
header('Referrer-Policy: no-referrer');
4647
header('Location: ' . $imgs_array[$id]);
4748
}
4849

test/deno.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function handler(req: Request): Promise<Response> {
2323
// return new Response(null, { status: 404 });
2424
return await fetch("https://deno.land/favicon.ico");
2525
}
26-
const searchParams = new URLSearchParams(url.search);
26+
const { searchParams } = url;
2727
try {
2828
let stringNumber: string; // 获取id
2929
const matched = url.pathname.match(/^\/(\d+)\.(?:jpg|jpeg|png|gif|webp)$/);

test/develop.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
import http from "http";
33
import handler from "./node.mjs";
44
const port = process.env.PORT || 8000;
5-
console.log(`Listening on ${port}`);
5+
console.log(`Server is running at http://localhost:${port}`);
66
http.createServer(handler).listen(port);

test/node.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from "axios";
2+
import { url } from "inspector";
23
// 需要先安装 axios
34
// 填入环境变量,或者修改下面的地址,这个地址应该返回一个文本文件,每行一个图片地址
45
const recordURL = process.env.RECORD_URL || "https://raw.githubusercontents.com/YieldRay/Random-Picture/master/url.csv";
@@ -17,11 +18,11 @@ const imagesArray = ["https://http.cat/503"];
1718
export default async function (req /*: http.IncomingMessage*/, res /*: http.ServerResponse*/) {
1819
const url = new URL("http://localhost" + req.url);
1920
if (req.url === "/favicon.ico") {
20-
res.writeHead(503);
21+
res.writeHead(404);
2122
res.end();
2223
return;
2324
}
24-
const searchParams = new URLSearchParams(url.search);
25+
const { searchParams } = url;
2526
let stringNumber; // 获取id
2627
const matched = url.pathname.match(/^\/(\d+)\.(?:jpg|jpeg|png|gif|webp)$/);
2728
if (matched) {

vercel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"functions": {
33
"api/index.php": {
4-
"runtime": "vercel-php@0.4.0",
4+
"runtime": "vercel-php@0.5.0",
55
"excludeFiles": "{test/**}",
66
"memory": 1024,
77
"maxDuration": 10

0 commit comments

Comments
 (0)