jonathantorres/medium-sdk
Composer 安装命令:
composer require jonathantorres/medium-sdk
包简介
Open source SDK for integrating Medium's OAuth2 API.
README 文档
README
Open source SDK for integrating Medium's OAuth2 API into your PHP application. Please note that Medium's API is still on an early stage and this implementation is not final. Breaking changes will happen. This SDK is unofficial. Medium's API documentation can be found here.
Installation
composer require jonathantorres/medium-sdk
Authentication
Initialize the SDK with your client credentials:
use JonathanTorres\MediumSdk\Medium; $credentials = [ 'client-id' => 'CLIENT-ID', 'client-secret' => 'CLIENT-SECRET', 'redirect-url' => 'http://example.com/callback', 'state' => 'somesecret', 'scopes' => 'scope1,scope2', ]; $medium = new Medium($credentials);
You can also use the connect method.
use JonathanTorres\MediumSdk\Medium; $medium = new Medium(); $medium->connect($credentials);
Browser-based authentication
Request the authentication url, this url will take the user to medium's authentication page. If successfull, it will return an authorization code.
$authUrl = $medium->getAuthenticationUrl(); <a href="<?php echo $authUrl; ?>">Authenticate with Medium</a>
Grab the authorization code from the url and use the authenticate method to be able to make requests to the API. Now you should be able to start making requests.
$authorizationCode = $_GET['code']; $medium->authenticate($authorizationCode);
Generate a new access token
Access tokens are valid for 60 days. Once it expires, you can request a new access token using your refresh token. Refresh tokens do not expire. You can request a new access token using your refresh token.
$accessToken = $medium->exchangeRefreshToken($refreshToken); $medium->setAccessToken($accessToken);
If you need to retrieve your refresh token after authentication, you can use the getRefreshToken(); method.
$refreshToken = $medium->getRefreshToken();
Authenticating with a self-issued access token
Medium recommends to use browser-based authentication, but you can also make requests to the API using a self-issued access token generated from your Medium settings page. These types of tokens never expire. Once you have it you can authenticate using this access token.
$medium = new Medium('SELF-ISSUED-ACCESS-TOKEN');
You can also use the connect method.
$medium = new Medium(); $medium->connect('SELF-ISSUED-ACCESS-TOKEN');
Now you should be ready to start making requests to the API using your self issued access token.
Users
Get the authenticated user details.
This will return an object with the user's details:
$user = $medium->getAuthenticatedUser(); echo 'Authenticated user name is: ' . $user->data->name;
Publications
List the specified user publications
This will return an array of objects that represent a publication that the specified user is related to in some way.
$publications = $medium->publications($userId)->data; foreach($publications as $publication) { echo 'Publication name: ' . $publication->name; }
List the contributors for the specified publication
This will return an array of users that are allowed to publish under the specified publication.
$contributors = $medium->contributors($publicationId)->data; foreach($contributors as $contributor) { echo 'User ' . $contributor->userId . ' is an ' . $contributor->role . ' on ' . $contributor->publicationId; }
Posts
Creating a post
This will create a post on the authenticated user's profile. Provide the id of the authenticated user and the post data. This will return an object with the created post's details.
$user = $medium->getAuthenticatedUser(); $data = [ 'title' => 'Post title', 'contentFormat' => 'html', 'content' => 'This is my post content.', 'publishStatus' => 'draft', ]; $post = $medium->createPost($user->data->id, $data); echo 'Created post: ' . $post->data->title;
Creating a post under a publication
This will create a post on the authenticated user's profile but also associate it with a publication. Provide the same data as creating a post. The response will also be the same with the exception of adding the publicationId field.
$data = [ 'title' => 'Post title', 'contentFormat' => 'html', 'content' => 'This is my post content.', 'publishStatus' => 'draft', ]; $post = $medium->createPostUnderPublication($publicationId, $data); echo 'Created post: ' . $post->data->title . ' under the publication ' . $post->data->publicationId;
Images
Uploading an image.
Provide an image resource, the image name and the extension. This will return an object with the uploaded image's data.
$imageResource = fopen('path/to/your/image', 'r'); $image = $medium->uploadImage($imageResource, 'image-filename.jpg'); echo 'Uploaded image ' . $image->data->url . ' succesfully.';
Running the examples
After cloning your repo:
git clone git@github.com:jonathantorres/medium-sdk-php.git
Add your API credentials on examples/credentials.php
$credentials = [ 'client-id' => 'YOUR-CLIENT-ID', 'client-secret' => 'YOUR-CLIENT-SECRET', 'redirect-url' => 'http://localhost:8888/callback.php', 'state' => 'secret', 'scopes' => 'basicProfile,publishPost,listPublications', ];
Start the included php server on the examples folder:
cd medium-sdk-php/examples && php -S localhost:8888
Run tests
Tests are written with PHPUnit.
After cloning your repo:
git clone git@github.com:jonathantorres/medium-sdk-php.git
Generate a self-issued access token from your Medium settings page. You need this to run the integration tests. Then, just run composer test on the project's root directory:
cd medium-sdk-php export MEDIUM_TOKEN=YOUR_ACCESS_TOKEN; composer test
Laravel Service Provider
A Service Provider for the Laravel Framework is now available, you can grab it here!
Changelog
Please see CHANGELOG for more information.
Contributing
Please see CONTRIBUTING for more details.
License
Licensed under the MIT license. Please see License file for more information.
jonathantorres/medium-sdk 适用场景与选型建议
jonathantorres/medium-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33.48k 次下载、GitHub Stars 达 79, 最近一次更新时间为 2015 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「sdk」 「medium」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jonathantorres/medium-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jonathantorres/medium-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jonathantorres/medium-sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Medium editor widget for Yii2
Laravel Service Provider for Medium's SDK for PHP.
A PSR-7 compatible library for making CRUD API endpoints
Medium editor widget for Yii2
Easy SDK for reading Medium posts of a user by parsing mediums JSON feed.
Social Buddy is a CraftCMS plugin for seamless social media publishing. Share entries to Facebook, Instagram, Pinterest, Telegram, Medium, LinkedIn, and X (Twitter), with options for both automatic and manual posting. Supports multi-platform publishing, customizable content, and cron job automation.
统计信息
- 总下载量: 33.48k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 80
- 点击次数: 11
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-20