https://github.com/nawab69/steemtools
Intermediate
Hello Steemians,
I am Nawab. Two months ago, I have started steemtools series. In this ongoing series, I have taught you to make some useful tools based on steemit in php language.
I can't make any tutorial in two months due to my some personal problems. Now I am coming back with some new tutorial with PHP & steem.
Today I will teach you about how to make a REST API from steemit API node.
Before making any API you need a database or a parent API. For making REST API with steem blockchain, you have to use api.steemit.com
Here is all information about steemit API.
| API URL | api.steemit.com |
|---|---|
| API METHOD | REQUEST |
| JSON_RPC | 2.0 |
| Method | condenser_api |
To help with the transition, steemit created condenser_api, which contains all of the API methods that currently exist and uses the existing argument formatting. The easiest way to get your app to work with Appbase is to change the API to condenser_api.
All calls in condenser_api will return [] as the argument, as the array argument passing is opaque and implemented in the API calls themselves. They follow the current argument formatting. Existing apps should only need to skip using login_api and send all of their calls to condenser_api without any other changes required to use Appbase.
Source : https://developers.steem.io/apidefinitions/#apidefinitions-condenser-api
First, Open your server folder using file manager in cpanel.
Then, create a folder in your hosting and give a name "API".
Then create a file inside the "API" folder and named it functions.php.
Now open the functions.php file with a text editor and write down the following code.
Start php code
keep the api.steemit.com ( your parent api) into a variable.
$api="https://api.steemit.com"; //parent api
GetData(). It has two input, first is API, second is post data.function GetData($api,$pdata){
// insert your php code here
}
write down this code inside the {}
$ch = curl_init($api); // steemit api
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pdata); //post data
curl_setopt($ch, CURLOPT_HTTPHEADER, false); // http header request
$data = curl_exec($ch);//get data from steemit API
curl_close($ch); //close curl
So decode JSON :
$arr = json_decode($data,true); //decode data
result array.$info = $arr[result]; //select result array from JSON
The functions.php file is here
In this part, I will teach you only one endpoint. Its get_account_count.
Create a file name get_account_count.php…
Now write down this code;
<?php
require('functions.php'); //include steemTools Library function
$array = array('jsonrpc' => '2.0','method' => 'condenser_api.get_account_count','params' =>[],'id' =>'1');
// create array
$post = json_encode($array, JSON_PRETTY_PRINT); // encode php array to JSON
GetData($api,$post); // Get Data function
$total = array('total_account' => $info);
$json = json_encode($total, JSON_PRETTY_PRINT); // encode php array to JSON
header("Access-Control-Allow-Origin: *"); header('Content-Type: application/json');
echo $json;
// print the JSON
Now save the files and browse the URL
yoursite/api/get_account_count. You will see the result.
https://github.com/nawab69/steemtools/blob/master/API/functions.php
https://github.com/nawab69/steemtools/blob/master/API/get_account_count.php