[New Project]
SteemConnect SDK for PHP is a simple and powerful PHP library which allows creating STEEM powered applications using PHP through SteemConnect.
This library allows broadcasting Steem operations through SteemConnect into the Steem blockchain.
0. Notice.
This is an Alpha release, which is not yet, fully documented or audited. Even the keys being handled by SteemConnect, I do not advise immediate production usage.
1. Usage.
This post will include basic usage instructions, which alone provide a good in-depth overview of its capabilities.
1.1 Configuration & Authentication:
The authentication flow, provided by this library, are bindings to the OAuth2 client itself, built by me at hernandev/oauth2-sc2.
There is no need to use that library directly on your projects if using this PHP SDK.
Aside from OAuth configuration, now there are some other options, that can be configured on the SDK.
Defining the configuration object is really simple:
// alias the Config class.
use SteemConnect\Config\Config;
// oauth client id.
$clientId = 'my-app-username.app';
// oauth client secret.
$clientSecret = 'some-random-key-here';
// return url.
$returnUrl = 'http://my-steem-app.com:8080/login/callback';
// list of required scopes.
$scopes = [
'login', 'offline', 'vote', 'comment', 'comment_delete',
'comment_options', 'custom_json', 'claim_reward_balance',
];
// starts the configuration object, passing the client id and secret.
$config = new Config($clientId, $clientSecret);
// configure the return / callback URL.
$config->setReturnUrl($returnUrl);
// set the reqired scopes.
$config->setScopes($scopes);
So far, all the same as the OAuth2 standard client, but there are 2 new options:
// set the default community name, that will be used on comments metadata.
$config->setCommunity('hernandev-testing');
// set the default application name, also to be used on comments metadata.
$config->setApp('sc2-sdk-php');
When the config object is ready to be used, we can then, create an instance of the SDK.
$sdk = new Client($config);
Handling Authorization / return:
// get the URL to send the users that will authorize your app.
$sdk->auth()->getAuthorizationUrl();
// exchanging the authorization code by a access token.
$token = $sdk->auth()->parseReturn();
1.2. Broadcasting Operations.
Just as simple as doing the authorization flow, this library was designed with simplicity in mind.
After receiving the access token, your application can now, start broadcasting operations, on users behalf.
Notice that, you will need to set the Token instance, on the SDK instance, so the HTTP requests to the broadcast API gets the correct authentication:
// set the Token instance on the SDK instance.
$sdk->setToken($token);
The token, can be serialized and deserialized to json.
1.2.1. UpVote and DownVote
// alias the Vote operation.
use SteemConnect\Operations\Vote;
// upvoting / downvoting a post:
$vote = new Vote();
$vote
->account('user-account')
->on('someuser', 'some-post-from-someuser')
->upVote(10000);
//->downVote(10000);
// broadcasting the operation:
$response = $sdk->broadcast($vote);
This library supports a decimal percent convention, so instead of using 5000 for upvoting a post by 50%, you could also use it as:
->upVote(0.5);
1.2.2. Reblogging a Post.
Reblogging is really easy to acomplish.
use SteemConnect\Operations\Reblog;
$reblog = new Reblog();
$reblog->account('user-account')->reblog('my-friend-account', 'my-friend-post-permlink');
$response = $sdk->broadcast($reblog);
1.2.2. Follow / Unfollow.
Just as simple:
use SteemConnect\Operations\Follow;
$follow = new Follow();
// follow
$follow->account('user-account')->follow('an-account-i-like');
// unfollow
$follow->account('user-account')->unfollow('an-account-i-do-not-like');
$response = $sdk->broadcast($follow);
1.2.3. Posting / Commenting / Replying.
From the Blockchian perspective, a post is just a comment. there are some contextual differences but, in general, they are.
I chose to not abstract that too much into a separated class, but the fluent interface is simple enough to handle it all.
use SteemConnect\Operations\Comment;
use SteemConnect\Operations\CommentOptions;
$comment = new Comment();
$comment
->author('hey-its-me')
->category('introduceyourself')
->permLink('this-parameter-is-optional-if-not-set-it-will-be-created-from-title')
->title('This title is options, if not set, it will be parsed from the first 150 charts on body')
->body('your-markdown-or-html-here')
->tags(['steem', 'iscool', 'crypto', 'busy']);
// optionally, you can also start a comment_otions operation:
$options = new CommentOptions();
$options
->of($post)
->allowVotes(false)
->allowCurationRewards(false)
->maxAcceptedPayout(0)
->percentSteemDollars(1000);
// the broadcast method magically accept any number of arguments, so broadcasting multiple operations is easily accomplished.
$response = $sdk->broadcast($comment, $options);
1.2.4. Using an array notation:
If you don't like / understand the abstractions of the library, you can, just pass raw operation arrays to the operations:
// custom_json operation from array.
new SteemConnect\Operations\CustomJson($arrayOfParameters);
// now nameless operation (set it with ->name()).
new SteemConnect\Operations\Operation($arrayOfParameters);
1.3. Roadmap
Next steps for this projects are:
- Full documentation.
- Unit Testing.
- Full security Audity.
- Show Case Application.
1.3. How to contribute?
My Discord username is hernandev#5834, and just hernandev on almost any other social platform.
Posted on Utopian.io - Rewarding Open Source Contributors