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.
composer require jonathantorres/medium-sdk
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);
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);
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);
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.
This will return an object with the user's details:
$user = $medium->getAuthenticatedUser();
echo 'Authenticated user name is: ' . $user->name;
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;
}
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;
}
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;
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;
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.';
- Laravel Service Provider.
- Symfony Bundle.
- Integration tests (WIP)
Please see CHANGELOG for more information.
Please see CONTRIBUTING for more details.
Licensed under the MIT license. Please see License file for more information.