在冲浪时候发现大佬 @道发的基于 vercel 部署的 360 图源接口随机图片 APi 。看了下是请求 json 然后用参数来获取对应的图片然后返回图。
使用教程
在你根目录新建一个 php 文件,粘贴下方你需要的代码,保存就好。
请求过的 JSON 图片返回 较快
<?php // 随机选择分类 $categoriesJson = '{"errno":"0","errmsg":" 正常","consume":"2003","total":"18","data":[{"id":"36","name":"4K 专区","order_num":"110","tag":"","create_time":"2015-12-08 13:50:44"},{"id":"6","name":" 美女模特","order_num":"100","tag":"","create_time":"2011-10-29 17:49:27"},{"id":"30","name":" 爱情美图","order_num":"99","tag":"","create_time":"2012-11-23 10:49:25"},{"id":"9","name":" 风景大片","order_num":"98","tag":"","create_time":"2011-11-02 16:33:34"},{"id":"15","name":" 小清新","order_num":"85","tag":"","create_time":"2011-12-15 18:47:03"},{"id":"26","name":" 动漫卡通","order_num":"84","tag":"","create_time":"2012-07-27 17:17:42"},{"id":"11","name":" 明星风尚","order_num":"83","tag":"","create_time":"2011-11-02 17:38:58"},{"id":"14","name":" 萌宠动物","order_num":"75","tag":"","create_time":"2011-12-15 18:23:27"},{"id":"5","name":" 游戏壁纸","order_num":"74","tag":"","create_time":"2011-10-29 17:49:12"},{"id":"12","name":" 汽车天下","order_num":"72","tag":"","create_time":"2011-12-13 18:59:40"},{"id":"10","name":" 炫酷时尚","order_num":"70","tag":"","create_time":"2011-11-02 17:10:53"},{"id":"29","name":" 月历壁纸","order_num":"69","tag":"","create_time":"2012-11-23 09:19:54"},{"id":"7","name":" 影视剧照","order_num":"68","tag":"","create_time":"2011-11-02 15:22:39"},{"id":"13","name":" 节日美图","order_num":"67","tag":" 节日 端午 中秋 元旦 圣诞 清明 情人 春节 新年 2012","create_time":"2011-12-14 18:47:32"},{"id":"22","name":" 军事天地","order_num":"14","tag":"","create_time":"2012-05-29 15:10:04"},{"id":"16","name":" 劲爆体育","order_num":"12","tag":"","create_time":"2011-12-30 11:37:49"},{"id":"18","name":"BABY 秀","order_num":"10","tag":"","create_time":"2012-03-28 23:52:39"},{"id":"35","name":" 文字控","order_num":"9","tag":"","create_time":"2014-09-25 18:35:57"}]}'; $categories = json_decode($categoriesJson, true)['data']; $randomCategory = $categories[array_rand($categories)]; // 图片获取 $cid = $randomCategory['id']; $imageUrl = "http://wallpaper.apc.360.cn/index.php?c=WallPaper&a=getAppsByCategory&cid={$cid}&from=360chrome"; $imageResponse = file_get_contents($imageUrl); if ($imageResponse!== false) { $data = json_decode($imageResponse, true); if (!empty($data['data']) && count($data['data']) > 0) { $item = $data['data'][rand(0, count($data['data']) - 1)]; if (isset($item['url'])) { $imageData = file_get_contents($item['url']); header('Content-Type: image/jpeg'); echo $imageData; } else { header('Content-Type: application/json'); echo json_encode(['error' => 'No valid image URLs found']); } } else { header('Content-Type: application/json'); echo json_encode(['error' => 'No images found in the API response']); } } else { header('Content-Type: application/json'); echo json_encode(['error' => 'Failed to fetch images']); } ?>
实时请求 JSON 在返回图片 较慢
<?php // 获取分类 $categoriesUrl = "http://cdn.apc.360.cn/index.php?c=WallPaper&a=getAllCategoriesV2&from=360chrome"; $categoriesResponse = file_get_contents($categoriesUrl); if ($categoriesResponse === false) { header('Content-Type: application/json'); echo json_encode(['error' => 'Failed to fetch category list']); exit; } $categories = json_decode($categoriesResponse, true)['data']; $randomCategory = $categories[array_rand($categories)]; // 图片获取 $cid = $randomCategory['id']; $imageUrl = "http://wallpaper.apc.360.cn/index.php?c=WallPaper&a=getAppsByCategory&cid={$cid}&from=360chrome"; $imageResponse = file_get_contents($imageUrl); if ($imageResponse!== false) { $data = json_decode($imageResponse, true); if (!empty($data['data']) && count($data['data']) > 0) { $item = $data['data'][rand(0, count($data['data']) - 1)]; if (isset($item['url'])) { $imageData = file_get_contents($item['url']); header('Content-Type: image/jpeg'); echo $imageData; } else { header('Content-Type: application/json'); echo json_encode(['error' => 'No valid image URLs found']); } } else { header('Content-Type: application/json'); echo json_encode(['error' => 'No images found in the API response']); } } else { header('Content-Type: application/json'); echo json_encode(['error' => 'Failed to fetch images']); } ?>