adyg/php-imgur-api-client
最新稳定版本:4.0.0
Composer 安装命令:
composer require adyg/php-imgur-api-client
包简介
Imgur API v3 client
README 文档
README
Object Oriented PHP wrapper for the Imgur API.
Uses Imgur API v3.
Information
- Branch 1.x use Guzzle 3 (but is not maintained)
- Branch 2.x use Guzzle 5 (but is not maintained)
- Branch 3.x use Guzzle 6 and PHP >= 5.6
- Branch master use Guzzle 7 and PHP >= 7.4
Composer
Download Composer
$ curl -s http://getcomposer.org/installer | php
Add the library details to your composer.json
composer require j0k3r/php-imgur-api-client@^4.0
Install the dependency with
$ php composer.phar install
Basic usage
// 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>'; }
The API calls can be accessed via the $client object
$memes = $client->api('memegen')->defaultMemes();
Documentation
Basic information
This client follow the same tree as the Imgur API.
Here is the list of available endpoints: account, album, comment, custom gallery, gallery, image, conversation, notification, memegen & topic.
You can access each endpoint using the api() method:
$client->api('album'); $client->api('comment'); $client->api('customGallery'); // etc ...
All available methods for each endpoints are in the folder Api. They mostly follow the description name in the Imgur doc. Here are few examples:
// 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 ...
Uploading an image
If you want to upload an image you can use one of these solutions:
$pathToFile = '../path/to/file.jpg'; $imageData = [ 'image' => $pathToFile, 'type' => 'file', ]; $client->api('image')->upload($imageData);
or
$urlToFile = 'http://0.0.0.0/path/to/file.jpg'; $imageData = [ 'image' => $urlToFile, 'type' => 'url', ]; $client->api('image')->upload($imageData);
or
$pathToFile = '../path/to/file.jpg'; $imageData = [ 'image' => base64_encode(file_get_contents($pathToFile)), 'type' => 'base64', ]; $client->api('image')->upload($imageData);
Pagination
For any API call that supports pagination and is not explicitly available via the method parameters, it can be achieved by using the BasicPager object and passing it as the second parameter in the api() call.
$pager = new \Imgur\Pager\BasicPager(1, 10); $images = $client->api('account', $pager)->images();
Here is a real life example if you want to retrieve all your available images of an account:
$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(); }
This pager is really basic:
- You won't have information about how many pages are available
- If you request a non-existant page, you'll get an empty array
NOTE: /gallery endpoints do not support the perPage query string, and /album/{id}/images is not paged.
Please, read the Imgur doc about it.
Image id or Album id ?
When you got an Imgur link it's almost impossible to be 100% sure if it's an image or an album. That's why we have an endpoint which might fix that by first checking an id as an image and if it's fail, test it as an album:
$data = $client->api('albumOrImage')->find($id);
License
php-imgur-api-client is licensed under the MIT License - see the LICENSE file for details
adyg/php-imgur-api-client 适用场景与选型建议
adyg/php-imgur-api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.01k 次下载、GitHub Stars 达 74, 最近一次更新时间为 2014 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「imgur」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 adyg/php-imgur-api-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adyg/php-imgur-api-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 adyg/php-imgur-api-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Imgur API v3 client
A PSR-7 compatible library for making CRUD API endpoints
Library for using the Imgur API in PHP
Laravel wrapper for the Imgur API.
A wrap-up package to make works easier with imgur.
Imgur upload API with Laravel.
统计信息
- 总下载量: 11.01k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 74
- 点击次数: 14
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-02-18