ivkos / wallhaven
Wallhaven API - 搜索、筛选和下载壁纸
v2.3.0
2016-08-20 09:47 UTC
Requires
- php: >= 5.4.0
- guzzlehttp/guzzle: 6.*
- paquettg/php-html-parser: 1.7.0
This package is not auto-updated.
Last update: 2024-09-22 07:17:47 UTC
README
描述
这是一个 PHP 库,用于 Wallhaven,允许您通过方便的 OOP 语法搜索壁纸并获取相关信息。此外,此库还提供了下载单个壁纸或批量异步下载多个壁纸的功能,从而大大减少了下载时间。
要求
- PHP 5.4 或更高版本
- Composer
安装
在项目根目录下创建一个 composer.json
文件
{ "require": { "ivkos/wallhaven": "2.*" } }
运行 php composer.phar install
以下载库及其依赖项。
快速文档
将此行添加到包含 Composer 包
<?php require 'vendor/autoload.php';
初始化 Wallhaven
use Wallhaven\Category; use Wallhaven\Order; use Wallhaven\Purity; use Wallhaven\Sorting; use Wallhaven\Wallhaven; $wh = new Wallhaven();
如果您在 Wallhaven 上有账户,可以使用它登录并访问所有可用的壁纸
$wh = new Wallhaven('YOUR_USERNAME', 'YOUR_PASSWORD');
搜索
您可以使用 Wallhaven::filter()
方法搜索壁纸并对其进行筛选。它返回一个 Filter
对象,该对象充当以下方法的流畅接口
keywords()
– 搜索查询或 #tagname,例如"风景"
"#汽车"
categories()
– 分类,或多个分类作为位字段。例如Category::PEOPLE
Category::GENERAL | Category::PEOPLE
Category::ALL
(默认)- 简写为Category::GENERAL | Category::ANIME | Category::PEOPLE
purity()
– 纯度,或多个纯度作为位字段。例如Purity::SFW
(默认)Purity::SFW | Purity::SKETCHY
Purity::ALL
– 简写为Purity::SFW | Purity::SKETCHY | Purity::NSFW
sorting()
– 排序。可以是以下之一Sorting::RELEVANCE
(默认)Sorting::RANDOM
Sorting::DATE_ADDED
Sorting::VIEWS
Sorting::FAVORITES
order()
– 结果顺序。可以是以下之一Order::DESC
(默认)Order::ASC
resolutions()
– 分辨率。应为字符串数组,格式为 WxH,例如["1920x1080"]
["1280x720", "2560x1440"]
ratios()
– 比例。应为字符串数组,格式为 WxH,例如["9x16"]
["16x9", "4x3"]
pages()
– 从 Wallhaven 获取的页面数。单页通常包含 24、32 或 64 张壁纸。getWallpapers()
– 执行带有指定筛选器的搜索。
示例
$wallpapers = $wh->filter() ->keywords("#cars") ->categories(Category::GENERAL) ->purity(Purity::SFW) ->sorting(Sorting::FAVORITES) ->order(Order::DESC) ->resolutions(["1920x1080", "2560x1440"]) ->ratios(["16x9"]) ->pages(3) ->getWallpapers();
$wallpapers = $wh->filter() ->keywords("landscape") ->ratios(["16x9"]) ->pages(2) ->getWallpapers();
返回一个包含匹配上述条件的 Wallpaper
对象的 WallpaperList
对象。
可以像数组一样访问 WallpaperList
对象,使用 foreach
迭代,并且具有 WallpaperList::count()
方法。
// Get favorites count for the first wallpaper in the list $wallpapers[0]->getFavorites(); // Print resolutions of all wallpapers in the list foreach ($wallpapers as $w) { echo $w->getResolution() . PHP_EOL; } // Get the number of wallpapers in the list echo "There are " . $wallpapers->count() . " wallpapers!" . PHP_EOL;
壁纸信息
Wallpaper
对象具有许多方法,可提供有关壁纸的信息
getId()
getTags()
getPurity()
getResolution()
getSize()
getCategory()
getViews()
getFavorites()
getFeaturedBy()
- 如果未登录则不可访问getFeaturedDate()
- 如果未登录则不可访问getUploadedBy()
getUploadedDate()
getImageUrl()
getThumbnailUrl()
如果您知道壁纸的 ID,可以获取单个壁纸的信息
$w = $wh->wallpaper(198320); $w->getTags(); // ["cats", "closeups"] $w->getViews(); // int(3500)
您还可以从搜索结果中获取壁纸的信息
$wallpapers = $wh->filter()->keywords(...)->getWallpapers(); $wallpapers[0]->getId(); // int(103929) $wallpapers[0]->getFavorites(); // int(367)
下载
将单个壁纸下载到特定目录
$wh->wallpaper(198320)->download("/home/user/wallpapers");
从搜索结果批量下载壁纸
$wallpapers = $wh->filter()->keywords(...)->getWallpapers(); $wallpapers->downloadAll("/home/user/wallpapers");
您还可以创建一个 WallpaperList
,将特定的壁纸添加到其中,然后批量下载它们,如下所示
use Wallhaven\Wallhaven; use Wallhaven\WallpaperList; $wh = new Wallhaven(); $batch = new WallpaperList(); $batch[] = $wh->wallpaper(198320); $batch[] = $wh->wallpaper(103929); $batch->downloadAll("/home/user/wallpapers");