0

First of all, I want to thank you all, I have learnt so much just by reading posts on this site. My name is Jonathan, and I am a fresh, new learner (& self learner) of PHP so my question might a bit silly for some of you who are great users :) I try to do a "foreach" request and I can't find the right way to get the result I expect, please see below my code :

    <?php

/*
    Plugin Name: coingecko widget
    Description: crypto widget using coingecko API
    Author: Jonathan
    Version: 1.0
    */

$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => "https://api.coingecko.com/api/v3/search/trending",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
]);

$response = var_dump(json_decode(curl_exec($curl),true));

    foreach($response as $responses) {
    echo $responses->{'coins'}->{0}->{'item'}->{'symbol'};
}
?>

And here is the result I get :

The result I get with my php code

I want to only get on my screen the symbol and the picture but it doesn't work as I still get the entire code on my screen and as you will see on the picture. I hope I am clear enough, really sorry again for the question which might be a bit silly, I have tried with {}, [], (), with different path and by excluding the "0" which is an array but I don't get what I want.

Many thanks in advance for your help, Have a great day ! Jonathan

  • 1
    What's that `var_dump()` in your code? That makes little sense. – KIKO Software Mar 10 '22 at 09:32
  • No question needs the introductory chitchat that this question contains. `->{'coins'}->` can be simplified to `->coins->`. If you were accessing an object ...but you are not. – mickmackusa Mar 10 '22 at 09:34

0 Answers0