SteemConnect SDK for PHP [alpha]
[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
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @hernandev I am @utopian-io. I have just upvoted you!
Achievements
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
what a wonderful post it is...i am impressed to see your post....so thanks for sharing with us...keep it up
Well, this is makes many things easier than a normal :) Well done @hernandev.
thank you @omeratagun! the more Steem based we have, the better for our community. We're just getting started!
So could this be used to connect other apps to this specific platform?
@derekfreeman this can be used to operate an steem account from a PHP application.
Let's say you have a wordpress blog, you can use this library to write a wordpress plugin that posts from wordpress to Steem.
But, not limited to, imagination is your friend :)
How about backwards?
Not sure I get what you mean by backwards, can you elaborate a little bit more?
If you reffering to power a wordpress website, with steem content, then, it's already doable by existing tools.
But a more native approach on PHP would be nicier, so not depend on nodejs or javascript.
if you need more ideas on it and wanna chat about it, poke me on Discord, I'll help the best way I can.
I guess, like being able to take content from a wordpress account, link it to your steemit account and allow them both to be utilized with a single application. I guess like putting dtube and steemit together with a wordpress blog on a single platform.
This is awesome! I see there is a lot of magic in the lib to make things more flexible and easier for development that's really cool. Great work on this library!
Thanks, put a lot of work into making it the more sugar-syntax possible. Next steps would be writing full documentation (with richer examples) and a simple demonstration application.
Also, thanks for all the help with my questions about the internals, would not be possible without your help!
Very good post! Keep up the good work and steem on!
https://smartsteem.com?r=steem3
You really have to see the smile on my face just reading this. @Hernandev, I really love this. Thanks man.
I am happy to see, read and enjoy your post amazingly innovative. Thanks for sharing.
Transfer 0.200 SBD or 0.250 steem to @mrbean1 and put the link of your post in the public memo I GIVE you FOLLOW AND 5 UPVOT and resteem by @mrbean1
click here to read more about me