Post to Steemit.com using PHP

in #utopian-io8 years ago (edited)

Preface

I am very new to Steemit.com and also to blockchain technology in a sense that I know very little. From the day I registered to Steemit.com, I wanted to make an application on top of Steem Blockchain. I only know PHP and very little JS. I found SteemJS library on Github but always wanted a PHP library. A few days ago I found php-graphene-node-client - an awesome libary by a fellow steemian. I also want to contribute, thats why I am making this tutorial.

What Will I Learn?

You'll learn about how to install PHP package using composer, and use it to post an article to Steemit.com. A summary is listed below.

  • Installing t3ran13/php-graphene-node-client
  • Installing bitcoin-core/secp256k1 and Bit-Wasp/secp256k1-php
  • Coding a PHP page to post to Steemit.com

Requirements

  • Awesome t3ran13/php-graphene-node-client PHP package
  • bitcoin-core/secp256k1 and Bit-Wasp/secp256k1-php library
  • Basic knowledge of Steemit.com platform
  • Basic coding knowledge of HTML and PHP
  • Basic Terminal knowledge

Difficulty

  • Intermediate

Tutorial Contents

First of all lets see what we are going to make.

Screenshot from 2018-02-17 14-03-06.png

In this tutorial I'll assuming you are using Ubuntu 16.04 and I'll show it in my Ubuntu 16.04 machine.

First of all we need to install php-graphene-node-client. Create a directory in your localhost or other web serving directory. Open up Terminal in that directory and copy and paste the following command.

composer require t3ran13/php-graphene-node-client

This will create a composer.json file. Now write composer install and press enter to install this package and its dependencies. We are good to go if we only want to read Steem Blockchain but we also want to broadcast to the blockchain. To write to Steem Blockchain we also need secp256k1, secp256k1-php and GMP modules. This two will be required to sign the transaction to be accepted by the Steem Blockchain.

To install this we need to download secp256k1 and secp256k1-php and extract them to secp256k1 and secp256k1-php folder respectively.

You will need source build tools.

sudo apt-get install build-essential checkinstall

To install libsecp256k1:

cd secp256k1 && ./autogen.sh && ./configure --enable-experimental --enable-module-{ecdh,recovery} && make && sudo make install && cd ../

To install secp256k1-php:

cd secp256k1-php &&  phpize &&  ./configure --with-secp256k1 && make && sudo make install && cd ../

Open /etc/php/7.0/apache2/php.ini and add extension=secp256k1.so at the bottom of the file.

To install GMP:

sudo apt install php7.0-gmp libgmp-dev

The environment setup is complete now we are ready to code. Create index.php in our project home and put following contents into it.

<?php
require_once 'vendor/autoload.php';

use GrapheneNodeClient\Tools\ChainOperations\OpComment;
use GrapheneNodeClient\Connectors\Http\SteemitHttpConnector;

$connector = new SteemitHttpConnector();
?>

In the above lines we are auto loading our dependencies and using OpComment and HttpConnector classes and then we are opening a new connection using SteemitHttpConnector().

It is time for adding rest of the lines of code to our index.php.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Post to Steemit</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="navbar-collapse" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="./">Post to Steemit</a>
            </div>

            <div class="collapse navbar-collapse" id="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="https://steemit.com">Steemit.com</a></li>
                    <li><a href="https://steemd.com">Steemd.com</a></li>
                </ul>
            </div>
        </div>
    </nav>
    
    <main class="container">

        <?php if( $_SERVER['REQUEST_METHOD'] === "GET" ) : ?>

            <form action="index.php" method="post" class="form-horizontal">
                <div class="form-group">
                    <div class="col-sm-8">
                        <label for="title">Title</label>
                        <input type="text" id="title" name="title" class="form-control" required="">
                        <p class="help-block">Enter post title, post permlink will be created from it.</p>
                    </div>
                    <div class="col-sm-4">
                        <label for="category">Category</label>
                        <input type="text" id="category" name="category" class="form-control" required="">
                        <p class="help-block">Enter main post category. E.g. steemit or steem or cryptocurrency.</p>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-12">
                        <label for="body">Body</label>
                        <textarea id="body" name="body" class="form-control" rows="10" required=""></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-4">
                        <label for="username">Steemit Username</label>
                        <input type="text" id="username" name="username" class="form-control" required="">
                        <p class="help-block">Enter Steemit username without @ sign.</p>
                    </div>
                    <div class="col-sm-8">
                        <label for="postingkey">Posting Private Key</label>
                        <input type="password" id="postingkey" name="postingkey" class="form-control" required="">
                        <p class="help-block">Enter private posting key. Get yours by going to https://steemit.com/@username/permissions</p>
                    </div>
                </div>
                <button type="submit" class="btn btn-lg btn-block btn-primary">Post</button>
            </form>

        <?php elseif ( $_SERVER['REQUEST_METHOD'] === "POST" ) : ?>

            <?php
            $answer = OpComment::doSynchronous(
                $connector,
                trim($_POST['postingkey']),
                trim($_POST['username']),
                strtolower(str_replace(' ', '-', trim($_POST['title']))),
                trim($_POST['title']),
                trim($_POST['body']),
                '{ "tags": ["steemit","test"], "app": "CodeBull/1.0"}',
                trim($_POST['category'])
            );
            ?>
            <?php if($answer) : ?>

                <div class="alert alert-success" role="alert">
                    <p>Your post has been posted to <a href="https://steemit.com/@<?php echo trim($_POST['username']);?>">Steemit.com</a>. You can also check it in <a href="https://steemd.com/tx/<?php echo $answer['result']['id']; ?>">Steemd.com</a></p>
                </div>

            <?php endif; ?>

        <?php endif; ?>

    </main>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>

In the markup we are using Bootstrap 3 to create a form and style it. We are also using inline PHP to check for what types of request we are getting to function accordingly. For $_GET request we are serving the form and for $_POST request we are processing form submission.

Article submission to the Steem Block cgain are done by this part of the code.

<?php
$answer = OpComment::doSynchronous(
    $connector,
    trim($_POST['postingkey']),
    trim($_POST['username']),
    strtolower(str_replace(' ', '-', trim($_POST['title']))),
    trim($_POST['title']),
    trim($_POST['body']),
    '{ "tags": ["steemit","test"], "app": "CodeBull/1.0"}',
    trim($_POST['category'])
);
?>

We are using OpComment class to make a synchronous transaction with the Steem Blockchain. First parameter is the connection we opened very top of the page, then we have posting private key, steemit username, post's permalink, post's title, body, custom json, and post's main category.

If our submission is successful we will see something like this.

Screenshot from 2018-02-17 15-39-45.png

You can find the codes in this Gist

Conclusion

This is a very basic implementation of this awesome PHP library. It can be extended to do many other things. I am hoping to make a WordPress Plugin using this.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

congratulations on such a great article - looks like the natives like you! I started to read it , but I don't have the time right now to devote my brain to it - so I will look at it tonight after I get back home - I am very interested in it though -it's great that that you took the time time put up something so valuable. I hope that I am able to use it to make something cool...

Thank you so much. Need and willingness to explore drive me to devote my time behind these. I hope you'll be able to use it in your projects.

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by reazuliqbal from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

This post has received a 0.50 % upvote from @drotto thanks to: @reazuliqbal.

Your Post Has Been Featured on @Resteemable!
Feature any Steemit post using resteemit.com!
How It Works:
1. Take Any Steemit URL
2. Erase https://
3. Type re
Get Featured Instantly � Featured Posts are voted every 2.4hrs
Join the Curation Team Here | Vote Resteemable for Witness

Hey @reazuliqbal I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • This is your first accepted contribution here in Utopian. Welcome!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

You got a 2.08% upvote from @luckyvotes courtesy of @reazuliqbal!

Do checkout https://steemit.com/steem/@gokulnk/social-post-steem and let me know if you are interested in taking a dig at creating a Drupal module for the same. If you can get a working version up I can up the Git prize as well :)

Hi, thanks you for commenting. I do not know how to make a Drupal module. But I'll look into it in my spare time.

I am looking to impliment something like this, and this is a great start.... Question: What about images? If my Body post includes an image embed will that image become the default blog post image? Will it be uploaded to steam and saved as if I pasted it into the post window on steemit.com?

If not how could I extend this example to add an image upload for the primary blog image?

Also how could we add more tags 4 more other then the primary one used as the category? Any info you could share would be great!