How to get API datas via using PHP
utopian-io·@omeratagun·
0.000 HBDHow to get API datas via using PHP
#### 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](https://www.apachefriends.org/index.html) and download the XAMPP. This application will provide you "localhost" service and mysql. Please do not forget to download [Notepad++](https://notepad-plus-plus.org/download/v7.5.4.html) ( 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; ``` html 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. ``` php // 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. ``` php $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; ``` php $link = 'https://steemit.com/'.$parent_link.'/@'.$omeratagun.'/'.$permlink; echo $link; ```  Variable named $link has a value as ["last post"](https://steemit.com/dmania/@omeratagun/games-can-make-your-dreams-come-true-zg1hbmlh-wzw9w) - Create database named "myposts" and make a connection from the file;  Create a connection to the spesific database and input data of the variable. ``` php $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 ) ``` php $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. ``` php echo $parent_link; ``` Print as ;  Create new table named "projects" and run this code; ``` php $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 ) <br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@omeratagun/how-to-get-api-datas-via-using-php">Utopian.io - Rewarding Open Source Contributors</a></em><hr/>