API JSON Parsing in PHP
Submitted by Yorkiebar on Monday, September 29, 2014 - 08:52.
Introduction:
This tutorial will be on how to use an API which returns a JSON format of information in PHP.API? JSON?
An API stands for an Application Program Interface and is used by external applications to connect with prebuilt libraries or sources of information via the API making the connection between the two. JSON is a form of information formatting, and stands for JavaScript Object Notation. This is used to keep neat KVPs (Key Value Pairs) of information so they can be easily sorted and formatted later on.Our References:
For this tutorial I am going to be the G2a.com website's backend API to gather the current marketplace prices for a specific game. This is located at: https://www.g2a.com/marketplace/product/auctions/?id=256. As you can see if you go straight to that above page, we get the following data:- {"sessionId":"713cdf7f1d8b179439bdeae7c4b9a505","a":{"k_159277":{"f":"\u00a32.49","ci":"223128","p":"2.490752","ep":"3.19","a":"159277","it":"steam","t":null,"mt":null,"so":0,"r":"100","rc":19,"c":"cl","tr":25,"free_keys":1},"k_159192":{"f":"\u00a32.65","ci":"54644","p":"2.65472","ep":"3.40","a":"159192","it":"steam","t":"1","mt":"1","so":0,"r":"100","rc":75,"c":"tr","tr":78,"free_keys":1},"k_159130":{"f":"\u00a32.73","ci":"287103","p":"2.7328","ep":"3.50","a":"159130","it":"steam","t":"1","mt":"1","so":0,"r":"100","rc":2885,"c":"it","tr":2924,"free_keys":1},"k_159096":{"f":"\u00a32.97","ci":"82626","p":"2.96704","ep":"3.80","a":"159096","it":"steam","t":"1","mt":"1","so":0,"r":"98","rc":1850,"c":"be","tr":1903,"free_keys":1},"k_158879":{"f":"\u00a33.05","ci":"774824","p":"3.04512","ep":"3.90","a":"158879","it":"steam","t":"1","mt":"1","so":0,"r":"100","rc":1490,"c":"es","tr":1533,"free_keys":1},"k_130928":{"f":"\u00a33.51","ci":"952629","p":"3.505792","ep":"4.49","a":"130928","it":"game","t":"1","mt":"1","so":0,"r":"100","rc":35,"c":"fr","tr":64,"free_keys":98},"k_158028":{"f":"\u00a33.74","ci":"883821","p":"3.740032","ep":"4.79","a":"158028","it":"steam","t":"1","mt":"1","so":0,"r":"100","rc":151,"c":"ru","tr":162,"free_keys":3},"k_157995":{"f":"\u00a33.75","ci":"154074","p":"3.74784","ep":"4.80","a":"157995","it":"steam","t":"0","mt":"0","so":0,"r":"100","rc":222,"c":"hu","tr":228,"free_keys":2},"k_149475":{"f":"\u00a33.81","ci":"1155471","p":"3.810304","ep":"4.88","a":"149475","it":"steam","t":"1","mt":"1","so":"1","r":"100","rc":249,"c":"hu","tr":574,"free_keys":11},"k_131973":{"f":"\u00a33.82","ci":"239149","p":"3.818112","ep":"4.89","a":"131973","it":"game","t":"1","mt":"1","so":0,"r":"100","rc":5175,"c":"cz","tr":5037,"free_keys":9},"k_0":{"f":"\u00a36.63","p":"6.628992","a":0,"c":"","rc":"","r":"","tr":0,"n":"G2A.com"}},"w":0}
Obtaining The JSON:
Before we can process the JSON information via PHP, we need to get the JSON information. To do this, we first need the API link, which we have above. Then we need to decide on a method to get the page's source code, I will be using the prebuilt PHP function named 'file_get_contents' because it is simple, only takes one parameter, and doesn't require any additional settings.JSON_decode:
When I said earlier that we have the above JSON information now stored within our '$src' variable, that's not technically true. We have the data, but PHP doesn't know it's a JSON, to do this we are going to use another prebuilt function named 'json_decode'...Array Looping:
Now we want to count how many depths of arrays we have, we have three. {"sessionId": starts one. "a":{"k_159277": starts another. {"f":"\u00a32.49" starts a third one. By the time we get to a fourth array, the previous one has ended, so therefore we have three depths; },"k_159192":{ (We can tell whether the depth has started or ended by the direction of the curly brace {}). So now we want to loop through each array using foreach from the json object...- foreach ($json as $k=>$v) {
- }
- foreach($v as $key=>$value) {
- }
- }
- foreach($value as $arKey => $arValue) {
- }
- }
- if ($arKey == 'p') {
- echo $arValue . '<br/>';
- }
Finished!
Now when we run the file, we get the following price data...- 2.490752
- 2.65472
- 2.7328
- 2.96704
- 3.04512
- 3.505792
- 3.740032
- 3.74784
- 3.810304
- 3.818112
- 6.628992
- <?php
- foreach ($json as $k=>$v) {
- foreach($v as $key=>$value) {
- foreach($value as $arKey => $arValue) {
- if ($arKey == 'p') {
- echo $arValue . '<br/>';
- }
- }
- }
- }
- }
- }
- ?>
Add new comment
- 379 views