1

I'm trying to decode JSON format My API Endpoint is https://api.reliableserver.host/api/landings

And this is the output

{
    "success": true,
    "data": [
        {
            "id": 1,
            "primary_balance": "$4,184.37",
            "primary_currency": "USD",
            "secondary_balance": "¥0",
            "secondary_currency": "JPY",
            "tertiary_balance": "฿0.00",
            "tertiary_currency": "THB",
            "first_language": "ไทย",
            "second_language": "English",
            "footer_text": "a",
            "created_at": "2020-10-26T07:45:49.000000Z",
            "updated_at": "2020-10-28T05:31:04.000000Z",
            "deleted_at": null
        }
    ],
    "message": "Landings retrieved successfully"
}

I need to echo individual values, for example: Primary Balance: $4,184.37

I tried using this:

$url = "https://api.reliableserver.host/api/landings";
    
$obj = json_decode($url);
    
echo $obj>primary_balance;

But it didnt work, kindly guide me what am I doing wrong.

1
  • you need to fetch the values coming from that url first, then you decode the results. you could use file_get_contents or implement the cURL lib in PHP Commented Oct 28, 2020 at 6:04

2 Answers 2

2

You can do this way :

$url = '{"success": true,"data": [{"id": 1,"primary_balance": "$4,184.37","primary_currency": "USD","secondary_balance": "¥0","secondary_currency": "JPY","tertiary_balance": "฿0.00","tertiary_currency": "THB","first_language": "ไทย","second_language": "English","footer_text": "a","created_at": "2020-10-26T07:45:49.000000Z","updated_at": "2020-10-28T05:31:04.000000Z","deleted_at": null}],"message": "Landings retrieved successfully"}';
$obj = json_decode($url, true);
echo $obj['data'][0]['primary_balance'];
// output $4,184.37

Above code tested here

You need file_get_contents() method to get the JSON data from your given URL.

$url = "https://api.reliableserver.host/api/landings";
$obj = json_decode(file_get_contents($url), true);
echo $obj['data'][0]['primary_balance'];
// output $4,184.37
Sign up to request clarification or add additional context in comments.

5 Comments

thanks @sta I did not know about rextester until now
this is great, it worked perfectly. I used it for the landing page.
One question, I have another similar endpoint but with multiple data. I implemented your method and it worked to display the first chunk of data, but how do i go about doing a for each so i can display all the data and not just the first one. this is the endpoint in questions: api.reliableserver.host/api/recent_activities
@ElB. Its really hard to tell you without see the code, I suggest you to ask a new question, so that we can help you :)
Please check it here, I asked my friend at the office to post instead, cz my account is still new. stackoverflow.com/questions/64567941/…
0

Basically, you are not calling that api anywhere. If it is an open endpoint (without auth or headers, you can do file_get_contents() or I suggest you to use curl. Also, you need to check on response data structure, it has a 'data' key which is an array. so you need to use foreach to iterate on the 'data' key.

I have given a sample answer that should work if there is only 1 item in data.

    $url = "https://api.reliableserver.host/api/landings";
    $resp = file_get_contents($url);
    $obj= json_decode($resp);// will return in object form
    echo $obj->data[0]->primary_balance;

or

$url = "https://api.reliableserver.host/api/landings";
$resp = file_get_contents($url);
$obj= json_decode($resp, true); // will return in array form
echo $obj['data'][0]['primary_balance'];

json_decode()

1 Comment

what if i have more than one item in my data, how do i go about doing for each? this si the endpoint in questions api.reliableserver.host/api/recent_activities

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.