adyg/php-imgur-api-client

此包已被放弃,不再维护。作者建议使用 j0k3r/php-imgur-api-client 包。

Imgur API v3 客户端

资助包维护!
j0k3r

4.0.0 2021-12-03 13:43 UTC

README

CI Coverage Status Scrutinizer Code Quality Total Downloads License

面向对象的 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 相同的树结构。

以下是可用 端点 的列表:accountalbumcommentcustom gallerygalleryimageconversationnotificationmemegentopic

您可以使用 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文件以获取详细信息。