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

in utopian-io •  6 years ago  (edited)

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 URLapi.steemit.com
API METHODREQUEST
JSON_RPC2.0
Methodcondenser_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,$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 = $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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you for your contribution @nawab69.
After analyzing your tutorial we suggest the following points listed below:

  • Your tutorial is quite short for a good tutorial. We recommend you aim for capturing at least 2-3 concepts.

  • Using the first person in the tutorials makes it difficult to understand the tutorials. We suggest using the third person in your text.

  • It's important to have the code indent. The indent code makes it easy to read the code.

  • In your next tutorial improve the structure and explain in more detail the features that will teach the reader.

Thank you for your work in developing this tutorial.
Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @portugalcoin! Keep up the good work!

Hi @nawab69!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @nawab69!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Here this works:
https://gist.github.com/daslicht/208dec0bf6d9c4a75b702c10d302f530

Thats not valid php:
$info = $arr[result];