[php + SteemJS ] Web tool & Rest API building tutorial - part 4

in #utopian-io5 years ago

Repository

https://github.com/nawab69/steemtools

68747470733a2f2f63646e2e737465656d6974696d616765732e636f6d2f44516d61326936724675386678653348414373735941476e346757784e366f6942776f357378553634536f446b64512f494d475f32303139303133315f3131323733335f3433392e4a5047.jpeg

You will learn:

  • You will learn How to use steemjs API
  • You will learn How to create an API in PHP
  • You will learn How to create a PHP website and connect with your own API

Requirements

  • php server
  • knowledge of PHP, JSON, API

Difficulty

  • Advanced

Tutorial Contents

In my previous tutorial, You have learned how to get data by using steemJS API. But I didn't find any API there for blog words count and blog characters count. So I have created a small PHP function to calculate total blog words and characters. Now I will teach you to make a REST API.

Create a REST API

At first create a page named get_blog_count.php. Then write this code one by one.
First write this code.

<?php
$url = $_GET["url"];

$_GET["url"]; function will collect the value of your steemit blog url.

A steemit post URL has three part. First part is the domain name. The 2nd part after /@ is the username. the last part after the last / is a permalink. You need an only username and permalink for steemjs API.
So, write this code to get the start position of your username from url.

$ustart = stripos($url,"@") + 1;

Then add this line to get the length of your username.

$ulen = strripos($url,"/") - $ustart;

Then write this line to get the start position of permlink.

Now take the only username and Permalink from URL using substr function. So, add this line.

$username = substr($url, $ustart, $ulen ); 
$plink = substr($url, $tstart);

Now connect to SteemJS API. It is the same as my previous tutorials. You should use the curl function.

$api = "https://api.steemjs.com/get_content?author=$username&permlink=$plink";
// Initiate curl
$curl = curl_init();
// Will return the response, if false it print the response
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($curl, CURLOPT_URL,$api);
// Execute
$data=curl_exec($curl);
// Closing
curl_close($curl);

You will get data in JSON. If you print it you may get this result.
IMG_20190206_224226_565.JPG

So you have to convert it to php strings. Use JSON decode function.

$info = json_decode($data,true);

Now if you print $info you will see like this.
IMG_20190206_224211_562.JPG
Then put body and net_votes strings in variables.

$votes = $info[net_votes];
$body = $info[body];

you have to add those lines to count total words and characters.

$total_words = str_word_count($body);          //count total words of body strings

$total_characters = strlen($body);                   // count total length of body string/total characters.

For making an API you have to make an array

$array = array('name' => $username,'url' => $url,'total_words' => $total_words,'total_char' => $total_characters,'net_votes' => $votes);
                     // create array

Then encode it to JSON

$json = json_encode($array, JSON_PRETTY_PRINT);

You should set up header for cross domain visit.

header("Access-Control-Allow-Origin: *"); header('Content-Type: application/json');

Now print the json.

echo $json;

IMG_20190206_225459_459.JPG
You can access your api using this yoursite/get_blog_count.php?url=YOUR_BLOG_LINK

You should remove .php extension by .htaccess . Open .htaccess file and insert this code.

#remove php file extension-e.g. https://example.com/file.php will become https://example.com/file 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php [NC,L]

Then save the file.
Now you can access your API using this your site/get_blog_count?url=YOUR_BLOG_LINK

You can get the source code from here
Live API

Create a web tool

Now I am going to teach you building a web tool for this function. You can build your web tool by using your previously created API. You can also build your web tool by previous functions. I have already taught you the method to display the current withdraw routes in this tutorial series.
So lets start-
First create a file blog_count.php. Then connect the header files using this code.

<?php
// Include header file 
include ('include/header.php');
// Include navigation bar file 
include ('include/nav.php'); 
?>

Now write the simple bootstrap form -

  <form action="" method="post" >
      <div class="form-group">
      <label class="sr-only" > Blog URL</label> 
      <div class="input-group mb-2 mr-sm-2"> 
      <input type="text" class="form-control" value="<?php echo $link; ?>" name="link" placeholder="BLOG URL"> </div> </div>
<br>
<div class="form-group">
<button align="center" name="submit" class="btn btn-primary mb-2">Submit</button> 
</div>
</form>

In API part You have used GET method. In Form you should use post method. Write down this code.

<?php 
if($_POST){
$link = $_POST["link"];
$ustart = stripos($link,"@") + 1;                    //position of the first character of username
$ulen = strripos($link,"/") - $ustart;              // length of username
$tstart = strripos($link,"/") + 1;                      // position of the first character of Permalink
$username = substr($link, $ustart, $ulen );          // take the username from link
$plink = substr($link, $tstart);                                // take the permalink from link
$api = "https://api.steemjs.com/get_content?author=$username&permlink=$plink";                 //connect to steemjs
// Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$api);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
$info = json_decode($result,true);           //decode JSON from API
$body = $info[body];                              // select only body string from $info
$votes = $info[net_votes];
$total_words = str_word_count($body);          //count total words of body strings
$total_characters = strlen($body);                   // count total length of body string
?>

This code is same as the previous code.

If you don't want to use the same method two times. You can connect with your API too by CURL methood. So use this code,

<?php 
if($_POST){
$link = $_POST["link"];
$api = "yoursite/get_blog_count?url=$link";  //use your site domain
// Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$api);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
$info = json_decode($result,true);           //decode JSON from API
$votes = $info[net_votes];
$total_words = $info[total_words];          // total words
$total_characters = $info[total_char];                   // total length of post

Then print the result in a bootstrap card.

<div class="alert alert-success" role="alert"> <h4 class="alert-heading">Counter</h4> <p>Total Words: <b><?php
echo $total_words; ?> </b>
</p> 
<p> Total characters : <b> <?php echo $total_characters; ?> </b>
</p>
<p> Total upvotes : <b> <?php echo $votes; ?>
</p>
</div>
<?php
}
?>

Now include footer and save this file.

<?php
include ('include/footer.php');
?>

You may find the source code here
Live website

Open the page with the browser. Type your steemit post link in URL field and then submit. You will get the result.
IMG_20190206_225544_090.JPG
IMG_20190206_225520_987.JPG

Curriculum

Proof of Work Done

https://github.com/nawab69/steemtools
http://steemtools.cf
https://github.com/nawab69/steemtools/blob/master/blog_count.php
https://github.com/nawab69/steemtools/blob/master/API/get_blog_count.php

Sort:  
Loading...

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!

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.033
BTC 64029.44
ETH 3157.04
USDT 1.00
SBD 4.02