T
T
thunderbird27112018-03-18 22:26:39
PHP
thunderbird2711, 2018-03-18 22:26:39

How to correctly process received data from json string from cURL?


the essence is this, there is a site that gives out information from its database in this form ,"last":7556.195,"buy":7556.915,"sell":7550,"updated":1521400547}}
I need to create a class in which from this line there will be variables high, low, buy, sell with values ​​from this line

<?php
    //выбор валюты
    $currency_1 = 'btc';
    $currency_2 = 'usd';
    $cur = $currency_1.'_'.$currency_2;
    parsing($cur);
    function parsing($cur){
        $target_url = 'https://wex.nz/api/3/ticker/'.$cur;
        $ch = curl_init($target_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($ch);
        $json_string = (string)$res;
    $arr = json_decode($json_string,true)
    echo $arr->['high'];
    echo '<br/>';
    echo '<br/>';
    var_dump(json_decode($json_string,true));
    
    echo $json_string.'<br/>';
    }

I tried in different ways, I was able to parse the string, process it as a string (like writing it to a variable from this character before that I could), but I need to do it through json, that is, like $high = $arr->high; (<-does not work either)
my browser gives me Parse error: syntax error, unexpected 'echo' (T_ECHO) in C:\OSPanel\domains\test.st\parsing1.php on line 14 - this is exactly the output line.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
riot26, 2018-03-18
@riot26

You need to understand the basic concepts of the language you are working with. What is an object in PHP and what is an array? How to work with them? Spend a few evenings looking through the documentation.

echo $arr['btc_usd']['high']; // для понимания
echo $arr[$cur]['high']; // правильный вариант

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question