Post to Steemit.com using PHP
utopian-io·@reazuliqbal·
0.000 HBDPost to Steemit.com using PHP
#### 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](https://github.com/t3ran13/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.

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](https://github.com/t3ran13/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.
``` bash
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](https://github.com/bitcoin-core/secp256k1), [secp256k1-php](https://github.com/Bit-Wasp/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](https://github.com/bitcoin-core/secp256k1/archive/master.zip) and [secp256k1-php](https://github.com/Bit-Wasp/secp256k1-php/archive/master.zip) 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:
``` bash
cd secp256k1 && ./autogen.sh && ./configure --enable-experimental --enable-module-{ecdh,recovery} && make && sudo make install && cd ../
```
To install secp256k1-php:
``` bash
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:
``` bash
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
<?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.
``` html
<!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
<?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.

You can find the codes in this [Gist](https://gist.github.com/CodeBull/5b6bc9214946c10825f384b5ba4ba687)
#### 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.
<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@reazuliqbal/post-to-steemit-com-using-php">Utopian.io - Rewarding Open Source Contributors</a></em><hr/>👍 claraquarius, taylan, codebull, minnowsupport, pharesim, drotto, pheonike, edrivegom, stephen.king989, jhermanbeans, steemprentice, numpypython, decibel, gindor, rondonson, cryptohustler, myday, gamerveda, redpillproj, pomperipossa, qwasert, cifer, vladimir-simovic, resteemable, jimmyrai28, iqbaladan, utopian-io, reazuliqbal, jorlauski, dieley, peunda, puffin, haarshan36, maugly, torpedooptimist, needy, tojan97, rudrarajuravi, global.steem, valery17, muhammadhaseebgb, shahbaz1447, limbabi, italianguy, solarparadise, willvote, hivaids, housegod, tee-strings, aceh-redaksi, edvard-rub, shaffrimohd, jjohnson78, lifeinspired, luckyvotes, glafiramilorado, loevkerbadonapyz, steinmanhelena, adlerrudigerlad, rudolphhochid, froklovkolyanka, tetron, vote-up, ryacha21, linuslee0216, mdsafwankhan, steembasicincome, rrhasnat, linco, amnlive, gaottantacinque, beunconstrained,