0

I wanted to print the name of the coin (obtained from a JSON response of a site) + "test" when the page is loaded; the problem is that only "test" is printed as if it did not find the name of the coin.

PHP code:

<?php
     $coinbase = "https://api.coinmarketcap.com/v1/ticker";
     $array = array("/bitcoin","/ethereum");

     find();

     function find(){
         $coin = file_get_contents($GLOBALS["coinbase"].$array[1]);
         $coin = json_decode($coin, TRUE);
         $v = $coin['name']."test";
         echo $v;
     }
?>

JSON structure:

[
   {
      id: "bitcoin-cash",
      name: "Bitcoin Cash",
      symbol: "BCH",
      rank: "4",
      price_usd: "1042.72",
      price_btc: "0.114721",
      24h_volume_usd: "462221000.0",
      market_cap_usd: "17742232718.0",
      available_supply: "17015338.0",
      total_supply: "17015338.0",
      max_supply: "21000000.0",
      percent_change_1h: "1.59",
      percent_change_24h: "-4.49",
      percent_change_7d: "-14.31",
      last_updated: "1520950752"
   }
]
MONS7ER
  • 124
  • 10
  • Your payload is of an array type. To retrieve, for example, the first element, you would need something like $coin[0]['name']. – filpa Mar 13 '18 at 15:44
  • `$array` is not accessible in `function find()`. Read about [variable scope in PHP](http://php.net/manual/en/language.variables.scope.php) then forget everything about `$GLOBALS` or `global`. – axiac Mar 13 '18 at 15:44
  • I have already tried to do $coin [0] ['name'] but it does not work – MONS7ER Mar 13 '18 at 15:58
  • I also request you to read about variable scope in PHP as @axiac commented previously – A l w a y s S u n n y Mar 13 '18 at 16:05

2 Answers2

1

If I try like this I got the answer. I've printed the $coin for your clear understanding How can you easily access 2D array with its index 0 here.

  function find()
  {
    $coinbase = "https://api.coinmarketcap.com/v1/ticker";
    $array = array("/bitcoin","/ethereum");
    $coin = file_get_contents($coinbase.$array[1]);
    $coin = json_decode($coin, TRUE);
    //printing only for debug purpose 
    print '<pre>';
    print_r($coin);
    print '<pre>';
    $v = $coin[0]['name']."test";
    echo $v;
  }

  find();           

Output:

Printing it just for your clear understanding why I used $coin[0]['name'] index to get the name from 2D $coin array.

Array
(
    [0] => Array
        (
            [id] => ethereum
            [name] => Ethereum
            [symbol] => ETH
            [rank] => 2
            [price_usd] => 687.193
            [price_btc] => 0.0760364
            [24h_volume_usd] => 1696390000.0
            [market_cap_usd] => 67457446384.0
            [available_supply] => 98163757.0
            [total_supply] => 98163757.0
            [max_supply] => 
            [percent_change_1h] => -0.63
            [percent_change_24h] => -2.36
            [percent_change_7d] => -16.98
            [last_updated] => 1520955853
        )

)

This is what you want

Ethereumtest

N.B: Please note here the comment of https://stackoverflow.com/users/4265352/axiac carefully

$array is not accessible in function find(). Read about variable scope in PHP then forget everything about $GLOBALS or global

As per comment:

$coinbase = "https://api.coinmarketcap.com/v1/ticker";
$array = array("/bitcoin","/ethereum");
function find(){
    global $coinbase;
    global $array;
    $coin = file_get_contents($coinbase.$array[1]);
    $coin = json_decode($coin, TRUE);
    print '<pre>';
    print_r($coin);
    $v = $coin[0]['name']."test";
    echo $v;
}
find();
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • Tested and it doesn't work. I don't know what to do. – MONS7ER Mar 13 '18 at 15:53
  • 1
    Did you run my code snippet? @MONS7ER I am getting the desired result then you are not getting it? may be `file_get_content()` not able to get data for you. – A l w a y s S u n n y Mar 13 '18 at 15:57
  • @MONS7ER clearly see on my snippet I've made `$coinbase` and `$array` variable local. In your case *$array & $coinbase is not accessible in function find()*, so check it carefully – A l w a y s S u n n y Mar 13 '18 at 16:00
  • @MONS7ER I see you snap , you missed to declear variable `$coinbase` and `$array` before using it..that's why I told you to see my code snippet carefully. I just made the global variable local for easier use but you totally remove it :( – A l w a y s S u n n y Mar 13 '18 at 16:07
  • I can also not run it in a function and leave it completely global etc. Or am I wrong? – MONS7ER Mar 13 '18 at 16:15
  • @MONS7ER OK :) I've updated my answer again. plz have a look.On my first answer I just make it `local` for the sake of simplicity. Now I make it like your old `global` scope code format. Also urge you to read http://php.net/manual/en/language.variables.scope.php Let me know not it working or not? – A l w a y s S u n n y Mar 13 '18 at 16:19
  • It does not work and I did copy paste; there will be a problem with hosting or something. At this point I do not know what to do but to thank you for the time spent. I will reread the page you have linked to me for the third time. – MONS7ER Mar 13 '18 at 16:25
  • see this link https://eval.in/971229 here also the response is blank because `WARNING file_get_contents() has been disabled for security reasons on line number 7` that why it is only showing `test` – A l w a y s S u n n y Mar 13 '18 at 16:29
  • So the problem is not in the code but in the site that gives me the json? – MONS7ER Mar 13 '18 at 16:39
  • No the site gives json perfectly. it is your code that is not able to get json from the site. If `file_get_contents()` doesn't work , you can try with `curl`. See this link https://stackoverflow.com/a/6724479/3723368 – A l w a y s S u n n y Mar 14 '18 at 02:28
  • Set `error_reporting(~0); ini_set('display_errors', 1);` on your script – A l w a y s S u n n y Mar 14 '18 at 02:32
0

Try:

$coin = file_get_contents($GLOBALS["coinbase"].$array[1]);
$coin = json_decode($coin, TRUE);
$v = $coin[0]['name']."test";
echo $v;

This json is an array of ojects so you should access first the index of the array and then, the property of the object.

EDIT

$coin = file_get_contents("https://api.coinmarketcap.com/v1/ticker/ethereum");

Try to hardcode the url to test it.

legomolina
  • 1,043
  • 2
  • 13
  • 33
  • Is the `$coin` variable empty? If not, can you paste `var_dump($coin)` before trying the json_decode and after that? – legomolina Mar 13 '18 at 15:53
  • before bool(false) after NULL – MONS7ER Mar 13 '18 at 15:56
  • This is the problem. file_get_contents() returns false if can't find the resource, so try modifying the url. – legomolina Mar 13 '18 at 15:57
  • The url is correct, coinbase = "https://api.coinmarketcap.com/v1/ticker" array [1] = "/ethereum" and the site is correct https://api.coinmarketcap.com/v1/ticker/ethereum – MONS7ER Mar 13 '18 at 16:00
  • @being-sunny is right, the probably error is that you are trying to access to `$array` inside the function. – legomolina Mar 13 '18 at 16:03