4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

SteemTools Tutorial -- Part 9 || Make a Restful Steem API with php

Repository

https://github.com/nawab69/steemtools

What Will I Learn?

  • You will learn about How to make an API
  • You will learn about rest API
  • You will learn how to make a small collection of API endpoints from a huge collection of API
  • You will learn how to make PHP functions
  • You will learn how to use STEEM API in PHP website.

Requirements

  • Knowledge of PHP
  • Knowledge on OOP
  • STEEM API FULL NODE
  • A hosting server (Linux server is best for PHP website)
  • A domain

Difficulty

Intermediate

Tutorial

53733567-91535c80-3eab-11e9-833d-9281203cf178.png

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

Parent API Information :

Here is all information about steemit API.

API URL api.steemit.com
API METHOD REQUEST
JSON_RPC 2.0
Method condenser_api

What is 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

Make a PHP function to get data from condenser API

First, Open your server folder using file manager in cpanel.

PicsArt_05-18-01.53.54.jpg

Then, create a folder in your hosting and give a name "API".

IMG_20190518_134934_599.JPG

Then create a file inside the "API" folder and named it functions.php.

IMG_20190518_134914_703.JPG

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
  • Then make a php function GetData(). It has two input,  first is API, second is post data.
function GetData(api,api,pdata){ 
// insert your php code here
}
  • Now you have to make a CURL function to get data from API.

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
  • You will get data from steemit API in JSON format. But there will be some extra data. You need only the "result".

An example of full data

IMG_20190518_133049_997.JPG

So decode JSON :

$arr = json_decode($data,true); //decode data 
  • Now you will get some array. But you should select only result array.
info=info =arr[result]; //select result array from JSON

IMG_20190518_134859_856.JPG

  • Now save this file. Your functions are ready for getting data from steemit API.

The functions.php file is here

Make an API endpoint

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.

IMG_20190518_131544_427.JPG

Curriculum

Proof Of work done

https://github.com/nawab69/steemtools/blob/master/API/functions.php

https://github.com/nawab69/steemtools/blob/master/API/get_account_count.php

https://api.steemit.com/get_account_count

42 upvotes $2.40

Replies (5)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: