PHP code example of intellexapps / pixabay-api-client
1. Go to this page and download the library: Download intellexapps/pixabay-api-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
intellexapps / pixabay-api-client example snippets
// Invoke the API directly by passing an array for the search
$response = (new VideoApi(API_KEY))->fetch(new VideoSearchParams([
'category' => Category::TRANSPORTATION,
'editors_choice' => true,
'per_page' => 3
]));
// Show images
foreach ($response->getVideos() as $video) {
echo $video->getMediumVideo()->getUrl() . PHP_EOL;
}
// Define search parameters using fluent setters
$search = (new ImageSearchParams())
->setColors([ Color::GREEN, Color::ORANGE ])
->setImageType(ImageType::PHOTO)
->setCategory(Category::NATURE)
->setEditorsChoice(true)
->setPerPage(3);
// Invoke the API
$response = (new ImageApi(API_KEY))->fetch($search);
// Show images
foreach ($response->getImages() as $image) {
echo $image->getURLForSize180() . PHP_EOL;
}
// Destination must be defined
if ($argc < 2) {
echo "Usage: php -f tests/examples/download.php <destination> <count>";
exit(1);
}
// Read the input parameters
$count = (int) min(50, $argv[2] ?? 50);
$destination = rtrim($argv[1], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
if (!is_dir($destination) || !is_writable($destination)) {
echo "Supplied destination is not a writable directory: ${destination}";
exit(2);
}
// Invoke the API
$response = (new ImageApi(API_KEY))->fetch(new ImageSearchParams([
PixabayParam::Q => 'kitten',
PixabayParam::CATEGORY => Category::ANIMALS,
PixabayParam::IMAGE_TYPE => ImageType::PHOTO,
PixabayParam::ORIENTATION => Orientation::HORIZONTAL,
PixabayParam::PER_PAGE => $count,
PixabayParam::SAFE_SEARCH => true,
PixabayParam::EDITORS_CHOICE => true,
PixabayParam::ORDER => OrderAlias::POPULAR
]));
// Download and store
$images = $response->getImages();
$count = count($images);
foreach ($images as $i => $image) {
$preview = explode('/', $image->getPreviewURL());
$name = end($preview) . PHP_EOL;
echo sprintf("%3s / %3s, %s", $i + 1, $count, $name);
Downloader::downloadTo($image->getLargeImageURL(), "{$destination}/{$name}");
}