PHP code example of jonathantorres / medium-sdk

1. Go to this page and download the library: Download jonathantorres/medium-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jonathantorres / medium-sdk example snippets


    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);

    use JonathanTorres\MediumSdk\Medium;

    $medium = new Medium();
    $medium->connect($credentials);

    $authUrl = $medium->getAuthenticationUrl();

    <a href=" echo $authUrl; 

    $authorizationCode = $_GET['code'];
    $medium->authenticate($authorizationCode);

    $accessToken = $medium->exchangeRefreshToken($refreshToken);
    $medium->setAccessToken($accessToken);

$refreshToken = $medium->getRefreshToken();

    $medium = new Medium('SELF-ISSUED-ACCESS-TOKEN');

    $medium = new Medium();
    $medium->connect('SELF-ISSUED-ACCESS-TOKEN');

    $user = $medium->getAuthenticatedUser();

    echo 'Authenticated user name is: ' . $user->data->name;

    $publications = $medium->publications($userId)->data;

    foreach($publications as $publication) {
        echo 'Publication name: ' . $publication->name;
    }

    $contributors = $medium->contributors($publicationId)->data;

    foreach($contributors as $contributor) {
        echo 'User ' . $contributor->userId . ' is an ' . $contributor->role . ' on ' . $contributor->publicationId;
    }

    $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;

    $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;

    $imageResource = fopen('path/to/your/image', 'r');
    $image = $medium->uploadImage($imageResource, 'image-filename.jpg');

    echo 'Uploaded image ' . $image->data->url . ' succesfully.';

    $credentials = [
        'client-id' => 'YOUR-CLIENT-ID',
        'client-secret' => 'YOUR-CLIENT-SECRET',
        'redirect-url' => 'http://localhost:8888/callback.php',
        'state' => 'secret',
        'scopes' => 'basicProfile,publishPost,listPublications',
    ];
bash
git clone [email protected]:jonathantorres/medium-sdk-php.git
bash
cd medium-sdk-php/examples && php -S localhost:8888
bash
git clone [email protected]:jonathantorres/medium-sdk-php.git
bash
cd medium-sdk-php
export MEDIUM_TOKEN=YOUR_ACCESS_TOKEN; composer test