0

How can I show JSON response in foreach loop:

$client = new Client();
$res = $client->get('https://api.exchangerate.host/symbols');
$data['symbols'] = $res->getBody();

return view('welcome', $data);

When I tried to show using loop then showing nothing Here is my loop

@foreach ($symbols as $symbol)
    <a href="" onclick="myfunction()">{{ $symbol }}</a>
@endforeach

And here is the response code

{
    "motd": {
        "msg": "If you or your company use this project or like what we doing, please consider backing us so we can continue maintaining and evolving this project.",
        "url": "https://exchangerate.host/#/donate"
    },
    "success": true,
    "symbols": {
        "AED": {
            "description": "United Arab Emirates Dirham",
            "code": "AED"
        },
        "AFN": {
            "description": "Afghan Afghani",
            "code": "AFN"
        }
    }
}

I want to show just symbols in the loop!

Hedayatullah Sarwary
  • 2,664
  • 3
  • 24
  • 38
MD Masum
  • 57
  • 3
  • 9

3 Answers3

2

json response should be decoded.

Try this

$data['symbols'] = json_decode($res->getBody())->symbols;
@foreach ($symbols as $symbol)
    <a href="" onclick="myfunction()">{{ $symbol['code'] }}</a>
@endforeach
habeebdev
  • 278
  • 2
  • 6
  • showing now Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given, called in – MD Masum Jun 01 '21 at 04:27
  • @MDMasum can you post complete error message? which line of code has this error? – habeebdev Jun 01 '21 at 04:44
  • $client = new Client(); $res = $client->get('https://api.exchangerate.host/symbols'); return $data['symbols'] = json_decode($res->getBody())->symbols; showing me this error Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given, called in – MD Masum Jun 01 '21 at 04:45
  • aah, that's different error. do, $data['symbols'] = json_decode($res->getBody())->symbols; return $data['symbols']; – habeebdev Jun 01 '21 at 04:49
  • or, like this return json_decode($res->getBody(), true)['symbols']; – habeebdev Jun 01 '21 at 04:50
0

You need to pass the correct information into the $data variable. Change from this

$data['symbols'] = $res->getBody();

to this

$data['symbols'] = $res->getBody()->symbols;

You also need to change the foreach loop as follows :-

@foreach ($symbols as $symbol)
    <a href="" onclick="myfunction()">{{ $symbol['code'] }}</a>
@endforeach
Will Walsh
  • 1,799
  • 1
  • 6
  • 15
  • Thank you will walsh, but getting this Cannot use object of type GuzzleHttp\Psr7\Stream as array error now – MD Masum Jun 01 '21 at 04:14
  • @MDMasum check the edit. I mistook the response body as an array rather than the GuzzleHttp\Psr7\Stream object. – Will Walsh Jun 01 '21 at 04:16
0

If JSON response is coming from the API, you can use PHP function json_decode() to decode. Here is the manual for the function. https://www.php.net/manual/en/function.json-decode.php