How to get API datas via using PHP

in #utopian-io8 years ago (edited)

What Will I Learn?

Here in this tutorial, i will explain how to get data via using steemit API's to help you in your future goals. Besides, i will explain how can you put those DATA's into mysql to record your previous "posts, post payouts,

  • How to fetch the API data via using couple of codes
  • How to get the data as array to print it
  • How to record variables into your mysql(Personal usage)

Requirements

First of all, you need a compiler to work with PHP. Please follow the link and download the XAMPP. This application will provide you "localhost" service and mysql. Please do not forget to download Notepad++ ( editor )

Difficulty

  • Intermediate

Tutorial Contents

To get an api examples and library; we should use https://v2.steemconnect.com/docs/steemjs.

In this web page, you can see api has been prepared very well. All examples, datas we may need exist over there. So, using the links under the spesific subject, we can get the information which we need to collect.

What that code mean ?

If i type to my browser this URL;


https://api.steemjs.com/get_discussions_by_blog?query={"tag":"omeratagun","limit":"10"}

I will get a respond like this;

Every action recorded by steemit is also given me there under that URL. By the mean "Everything about my posts".

Lets go to our editor(notepad++) And see how we can get these datas into our project.

// Create variable, get the data from API URL //
$omeratagun = "omeratagun";
$steemit_api_url_blog = 'https://api.steemjs.com/get_discussions_by_blog?query={"tag":"'.$omeratagun.'","limit":"50"}';
$json_get = file_get_contents($steemit_api_url_blog);
$data_decode = json_decode($json_get, true);
$blog_posts = $data_decode[0]['permlink'];
echo $blog_posts;
 

If i print the data now, i will get single result inreturn because i do not have any loop(can make dataflow continously). So lets see what i will get;

We just found out the link of our post but not all. So we need to find "parent link" which steemit assigned.


$parent_link = $data_decode[0]['parent_permlink'];
$permlink = $data_decode[0]['permlink'];

If we print these returns together, we will get a result like this;

This wouldnt be opened in the browser because of missing parts. As far as you can see, this has no meaning without seperators. We need, https://steemit.com/dmania/@omeratagun/e.g to get the link properly.

Lets do it;


$link = 'https://steemit.com/'.$parent_link.'/@'.$omeratagun.'/'.$permlink;

echo $link;

Variable named $link has a value as "last post"

  • Create database named "myposts" and make a connection from the file;

Create a connection to the spesific database and input data of the variable.


$connection = mysqli_connect("localhost","root","","myposts");

  • At last, we are ready to put our latest post link into our database ( No error codes or checks in this )

$add_post_link = "INSERT INTO my_posts (post_link) VALUES ('$link')";
mysqli_query($connection,$add_post_link);

Run the php file and check the result in your database,

What is the benefit of this tutorial ?

By using API of steemit, you will be able to collect all datas(voters, dates, links, payouts etc) and make your own project via using database to list it back again. You may make an statistic(e.g how many dmania posts you published, how many utopian etc).

How could you seperate dmania posts ?

We already have that data in $parent_link variable.


echo $parent_link;

Print as ;

Create new table named "projects" and run this code;


$add_parent = "INSERT INTO my_posts (projects) VALUES ('$parent_link')";
mysqli_query($connection,$add_parent);

If your latest post via utopian, it will show it as utopian and write into your database. Thanks for reading. See you in next parts ( if i can find time )



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Are these API endpoints always open by default to anyone? And who hosts it please?

I gave the link up there. Open for all by steemconnect. Thus, steemconnect the host.

Nice...I didn't know steemconnect hosts this service. I just always see "steemjs" code repository and relative endpoints but no host. It made me think I had to download the whole code repository and host it on my own server in order to gain access to those data

Yeah no need :)

Baba Sen BüYÜK bİr Hackersın iskorpitx i tanıyomusun

Oh man you are super brilliant. No one would ever have thought to share this idea. I am so impressed its not so easy to work on it. But you did. GREAT
Are you PHP developer?

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

good job :)

Was looking for something like that,thank you.

Hey @omeratagun I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x