Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ivkos/wallhaven4php

Wallhaven API for PHP - Search, filter and download wallpapers
https://github.com/ivkos/wallhaven4php

Last synced: 4 days ago
JSON representation

Wallhaven API for PHP - Search, filter and download wallpapers

Awesome Lists containing this project

README

        

Wallhaven API for PHP
===================
[![](https://img.shields.io/packagist/v/ivkos/wallhaven.svg?style=flat-square)](https://packagist.org/packages/ivkos/wallhaven)
[![](https://img.shields.io/packagist/dt/ivkos/wallhaven.svg?style=flat-square)](https://packagist.org/packages/ivkos/wallhaven)
[![](https://img.shields.io/github/license/ivkos/Wallhaven.svg?style=flat-square)](LICENSE)

## Description
A PHP library for **[Wallhaven](https://wallhaven.cc)** that allows you to search for wallpapers and get information
about them in convenient OOP syntax. Additionally, this library provides the ability to download individual
wallpapers, or batch download many wallpapers asynchronously which considerably reduces download times.

## Requirements
* PHP 5.4 or newer
* Composer

## Install
Create a `composer.json` file in your project root:
```json
{
"require": {
"ivkos/wallhaven": "2.*"
}
}
```

Run `php composer.phar install` to download the library and its dependencies.

## Quick Documentation
Add this line to include Composer packages:
```php
filter()
->keywords("#cars")
->categories(Category::GENERAL)
->purity(Purity::SFW)
->sorting(Sorting::FAVORITES)
->order(Order::DESC)
->resolutions(["1920x1080", "2560x1440"])
->ratios(["16x9"])
->pages(3)
->getWallpapers();
```

```php
$wallpapers = $wh->filter()
->keywords("landscape")
->ratios(["16x9"])
->pages(2)
->getWallpapers();
```
Returns a `WallpaperList` object containing `Wallpaper` objects that match the criteria above.

The `WallpaperList` object can be accessed like an array, iterated over using `foreach`, and has a `WallpaperList::count()` method:
```php
// 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 Information
The `Wallpaper` object has a number of methods that provide information about the wallpaper:

- `getId()`
- `getTags()`
- `getPurity()`
- `getResolution()`
- `getSize()`
- `getCategory()`
- `getViews()`
- `getFavorites()`
- `getFeaturedBy()` - not accessible if not logged in
- `getFeaturedDate()` - not accessible if not logged in
- `getUploadedBy()`
- `getUploadedDate()`
- `getImageUrl()`
- `getThumbnailUrl()`

You can get information about a single wallpaper if you know its ID:
```php
$w = $wh->wallpaper(198320);

$w->getTags(); // ["cats", "closeups"]
$w->getViews(); // int(3500)
```

You can also get information about wallpapers from a search result:
```php
$wallpapers = $wh->filter()->keywords(...)->getWallpapers();

$wallpapers[0]->getId(); // int(103929)
$wallpapers[0]->getFavorites(); // int(367)
```

### Downloading
To download a single wallpaper to a specific directory:
```php
$wh->wallpaper(198320)->download("/home/user/wallpapers");
```

To batch download wallpapers from a search result:
```php
$wallpapers = $wh->filter()->keywords(...)->getWallpapers();
$wallpapers->downloadAll("/home/user/wallpapers");
```

You can also create a `WallpaperList`, add specific wallpapers to it, and then batch download them, like so:
```php
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");
```

#### For more information, please refer to the source code and the PHPDoc blocks.