• We just launched and are currently in beta. Join us as we build and grow the community.

API JSON Parsing in PHP

kylekylee

Altcoin Explorer
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
171
Likes
158
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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:

  1. {

    "sessionId"

    :

    "713cdf7f1d8b179439bdeae7c4b9a505"

    ,

    "a"

    :

    {

    "k_159277"

    :

    {

    "f"

    :

    "\u

    00a32.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"

    :

    "\u

    00a32.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"

    :

    "\u

    00a32.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"

    :

    "\u

    00a32.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"

    :

    "\u

    00a33.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"

    :

    "\u

    00a33.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"

    :

    "\u

    00a33.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"

    :

    "\u

    00a33.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"

    :

    "\u

    00a33.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"

    :

    "\u

    00a33.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"

    :

    "\u

    00a36.63"

    ,

    "p"

    :

    "6.628992"

    ,

    "a"

    :

    0

    ,

    "c"

    :

    ""

    ,

    "rc"

    :

    ""

    ,

    "r"

    :

    ""

    ,

    "tr"

    :

    0

    ,

    "n"

    :

    "G2A.com"

    }

    }

    ,

    "w"

    :

    0

    }

We are going to sort this soon.

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.

  1. $src

    =

    file_get_contents

    (

    'https://www.g2a.com/marketplace/product/auctions/?id=256'

    )

    ;

Now we have the above JSON information stored within our '$src' variable. Next we want to take note of a couple of things;
You may have noticed already if you have some previous programming experience, that the JSON above is formatted in to a few arrays, this means that we will have to get through a few arrays before we can extract the KVP we want.

We also need to decide exactly what value we are grabbing, I will grab all of the prices, which are the values of the key 'p'. Here are a few examples from above;
"p":"2.490752"
"p":"2.65472"
"p":"3.74784"

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'...

  1. $json

    =

    json_decode

    (

    $src

    ,

    true

    )

    ;

The second parameter I have set to 'true'. This means it will return as an array, this makes it easier to format in a minute.

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...

  1. foreach

    (

    $json

    as

    $k

    =>

    $v

    )

    {

  2. }

Next we want to check if the '$v' object, which is the currently looping/iterating value/object, is an array, if so, we do another foreach loop through that array...

  1. if

    (

    is_array

    (

    $v

    )

    )

    {
  2. foreach

    (

    $v

    as

    $key

    =>

    $value

    )

    {

  3. }
  4. }

Finally we want to do it one more time for the third and final layer of depth...

  1. if

    (

    is_array

    (

    $value

    )

    )

    {
  2. foreach

    (

    $value

    as

    $arKey

    =>

    $arValue

    )

    {

  3. }
  4. }

We now simply output any price we find. To do this, we check the current key to see if it is equal to 'p' (the price key I mentioned from earlier within this tutorial), and if it is, we output the value...

  1. if

    (

    $arKey

    ==

    'p'

    )

    {
  2. echo

    $arValue

    .

    '<br/>'

    ;
  3. }

Finished!

Now when we run the file, we get the following price data...

  1. 2.490752
  2. 2.65472
  3. 2.7328
  4. 2.96704
  5. 3.04512
  6. 3.505792
  7. 3.740032
  8. 3.74784
  9. 3.810304
  10. 3.818112
  11. 6.628992

Here is the full source code...

  1. <?php
  2. $src

    =

    file_get_contents

    (

    'https://www.g2a.com/marketplace/product/auctions/?id=256'

    )

    ;
  3. $json

    =

    json_decode

    (

    $src

    ,

    true

    )

    ;
  4. foreach

    (

    $json

    as

    $k

    =>

    $v

    )

    {
  5. if

    (

    is_array

    (

    $v

    )

    )

    {
  6. foreach

    (

    $v

    as

    $key

    =>

    $value

    )

    {
  7. if

    (

    is_array

    (

    $value

    )

    )

    {
  8. foreach

    (

    $value

    as

    $arKey

    =>

    $arValue

    )

    {
  9. if

    (

    $arKey

    ==

    'p'

    )

    {
  10. echo

    $arValue

    .

    '<br/>'

    ;
  11. }
  12. }
  13. }
  14. }
  15. }
  16. }
  17. ?>


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top