All Projects → jonathantorres → medium-sdk-php

jonathantorres / medium-sdk-php

Licence: MIT License
Open source SDK for integrating Medium's OAuth2 API into your PHP application.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to medium-sdk-php

github-readme-medium
📖 Dynamically generated your latest Medium article on your GitHub readmes!
Stars: ✭ 56 (-29.11%)
Mutual labels:  medium
kuote
Create beautiful Medium-like quotes, perfect for sharing on Twitter or Instagram.
Stars: ✭ 11 (-86.08%)
Mutual labels:  medium
readtime
Calculates the time some text takes the average human to read, based on Medium's read time forumula.
Stars: ✭ 93 (+17.72%)
Mutual labels:  medium
blog-template
Template for the Jekyll-powered elementary blog
Stars: ✭ 34 (-56.96%)
Mutual labels:  medium
FlutterMediumClone
Building a Medium Clone in Flutter.
Stars: ✭ 55 (-30.38%)
Mutual labels:  medium
Medium-Python-Neural-Network
This code is part of my post on Medium.
Stars: ✭ 58 (-26.58%)
Mutual labels:  medium
rawr
Extract raw R code directly from webpages, including Github, Kaggle, Stack Overflow, and sites made using Blogdown.
Stars: ✭ 15 (-81.01%)
Mutual labels:  medium
medium-to-wordpress-migration
Script to export medium blogs to wordpress rss xml format
Stars: ✭ 15 (-81.01%)
Mutual labels:  medium
webpack-2.0-from-scratch
Guide on how to setup a Webpack config from scratch.
Stars: ✭ 72 (-8.86%)
Mutual labels:  medium
read-medium-extension
Chrome extension for reading Medium for free without leaving the page.
Stars: ✭ 55 (-30.38%)
Mutual labels:  medium
medium-sdk-elixir
Elixir SDK for the Medium.com API. 🌐
Stars: ✭ 19 (-75.95%)
Mutual labels:  medium
MediumAppFlutter
Flutter recreation of the UI of the Medium android app
Stars: ✭ 62 (-21.52%)
Mutual labels:  medium
React-Medium-Blog
React blog page which consist of last ten medium posts. The blog card is created using shards React and React application.
Stars: ✭ 14 (-82.28%)
Mutual labels:  medium
hosts
自动生成 Hosts 文件,科学上网
Stars: ✭ 30 (-62.03%)
Mutual labels:  medium
medium-unlocker
Read Medium content without limit!
Stars: ✭ 127 (+60.76%)
Mutual labels:  medium
BottomNavArchDemo
The demo project for Bottom Navigation with Navigation Architecture Components article
Stars: ✭ 53 (-32.91%)
Mutual labels:  medium
artigo-solid-medium
Implementações dos exemplos demonstrados no artigo: https://bit.ly/2o97vY1
Stars: ✭ 28 (-64.56%)
Mutual labels:  medium
zoom.ts
A lightweight TypeScript library for image zooming, as seen on Medium.
Stars: ✭ 44 (-44.3%)
Mutual labels:  medium
bypass-paywalls-chrome-clean-magnolia1234
Bypass Paywalls Chrome Clean (GitLab proxy)
Stars: ✭ 32 (-59.49%)
Mutual labels:  medium
code-medium
Browser extension that simplifies writing code in Medium posts. Quickly create and edit Github Gists without leaving the editor
Stars: ✭ 59 (-25.32%)
Mutual labels:  medium

Medium SDK for PHP

Build Status Version

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 [email protected]: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 [email protected]: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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].