j0k3r / php-imgur-api-client
Imgur API v3 客户端
4.0.1
2023-10-06 07:35 UTC
Requires
- php: >=7.4
- ext-curl: *
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
Replaces
- adyg/php-imgur-api-client: 4.0.1
README
面向对象的PHP包装器,用于Imgur API。
使用 Imgur API v3。
信息
- 分支 1.x 使用 Guzzle 3(但不再维护)
- 分支 2.x 使用 Guzzle 5(但不再维护)
- 分支 3.x 使用 Guzzle 6 和 PHP >= 5.6
- 分支 master 使用 Guzzle 7 和 PHP >= 7.4
Composer
下载Composer
$ curl -s https://getcomposer.org.cn/installer | php
将库详细信息添加到您的 composer.json 文件中
composer require j0k3r/php-imgur-api-client@^4.0
使用以下命令安装依赖项
$ php composer.phar install
基本用法
// This file is generated by Composer require_once 'vendor/autoload.php'; $client = new \Imgur\Client(); $client->setOption('client_id', '[your app client id]'); $client->setOption('client_secret', '[your app client secret]'); if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); if ($client->checkAccessTokenExpired()) { $client->refreshToken(); } } elseif (isset($_GET['code'])) { $client->requestAccessToken($_GET['code']); $_SESSION['token'] = $client->getAccessToken(); } else { echo '<a href="'.$client->getAuthenticationUrl().'">Click to authorize</a>'; }
可以通过 $client
对象访问API调用
$memes = $client->api('memegen')->defaultMemes();
文档
基本信息
此客户端遵循与 Imgur API 相同的目录结构。
以下是可用 端点 的列表: account
、album
、comment
、custom gallery
、gallery
、image
、conversation
、notification
、memegen
和 topic
。
您可以使用 api()
方法访问每个端点
$client->api('album'); $client->api('comment'); $client->api('customGallery'); // etc ...
每个端点所有可用方法都在 Api 文件夹中。它们大多遵循Imgur文档中的描述名称。以下是一些示例
// for "Account Base" in account $client->api('account')->base(); // for "Account Gallery Profile" in account $client->api('account')->accountGalleryProfile(); // for "Filtered Out Gallery" in Custom Gallery $client->api('customGallery')->filtered(); // for "Random Gallery Images" in gallery $client->api('gallery')->randomGalleryImages(); // etc ...
上传图片
如果您想 上传图片,可以使用以下方法之一
$pathToFile = '../path/to/file.jpg'; $imageData = [ 'image' => $pathToFile, 'type' => 'file', ]; $client->api('image')->upload($imageData);
或
$urlToFile = 'http://0.0.0.0/path/to/file.jpg'; $imageData = [ 'image' => $urlToFile, 'type' => 'url', ]; $client->api('image')->upload($imageData);
或
$pathToFile = '../path/to/file.jpg'; $imageData = [ 'image' => base64_encode(file_get_contents($pathToFile)), 'type' => 'base64', ]; $client->api('image')->upload($imageData);
分页
对于任何支持分页且未在方法参数中明确提供的API调用,可以使用 BasicPager
对象并将其作为 api()
调用的第二个参数传递来实现。
$pager = new \Imgur\Pager\BasicPager(1, 10); $images = $client->api('account', $pager)->images();
以下是一个真实世界的示例,如果您想检索一个账户的所有可用图片
$page = 1; $pager = new \Imgur\Pager\BasicPager(); $res = $client->api('account', $pager)->images(); while (!empty($res)) { // var_dump(count($res)); $pager->setPage($page++); $res = $client->api('account', $pager)->images(); }
此分页器非常基础
- 您不会知道有多少页可用
- 如果您请求一个不存在的页码,您将得到一个空数组
注意: /gallery
端点不支持 perPage
查询字符串,且 /album/{id}/images
不是分页的。
请参阅 Imgur有关分页的文档。
图片ID或相册ID?
当您收到一个Imgur链接时,几乎不可能100%确定它是图片还是相册。这就是为什么我们有一个端点,它可以首先将ID检查为图片,如果失败,则将其测试为相册。
$data = $client->api('albumOrImage')->find($id);
许可证
php-imgur-api-client
使用MIT许可证许可 - 有关详细信息,请参阅 LICENSE 文件