Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [AJAX][PHP] Jak pobrać jedną wartość z JSON string?
-gośćcc-
post
Post #1





Goście







Siema, mam taki output:
Kod
object(stdClass)#1 (2) { ["status"]=> string(7) "success" ["data"]=> object(stdClass)#2 (2) { ["code"]=> string(6) "eurpln" ["rate"]=> float(4.22852) } }


Jedyne, co potrzebuję to
Kod
4.22852




Obecny kod wygląda tak(usunąłem $apiKey bo to niby dla każdego usera osobny ma być):
  1. <html>
  2. <head>
  3. <title>PHP Test</title>
  4. </head>
  5. <body>
  6. <?php
  7.  
  8. $apiKey = 'xxxxxx5';
  9. $url = 'http://apiwalutowe.pl/api/';
  10. $query = array();
  11. $query['key'] = $apiKey;
  12. $query['code'] = 'EURPLN';
  13. // tutaj ewentualnie inne parametry tabeli poniżej
  14.  
  15. $url = $url . '?' . http_build_query($query);
  16.  
  17. $ch = curl_init();
  18. curl_setopt($ch, CURLOPT_URL, $url);
  19. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  20. $json = curl_exec($ch);
  21.  
  22. $info = curl_getinfo($ch);
  23.  
  24. if ($info['code'] != 200)
  25. {
  26. die('BŁĄD: ' . $json['message']);
  27. }
  28.  
  29. $data = json_decode($json);
  30.  
  31. var_dump($data);
  32. ?>
  33. </body>
  34. </html>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
chmiello
post
Post #2





Grupa: Zarejestrowani
Postów: 33
Pomógł: 5
Dołączył: 12.07.2010

Ostrzeżenie: (0%)
-----


a nie można po prostu dać dać do return czy echo :
  1. $data->rate;
Go to the top of the page
+Quote Post
-Gość-
post
Post #3





Goście







Nie bardzo rozumiem co masz nie myśli, a raczej gdzie?
Go to the top of the page
+Quote Post
chmiello
post
Post #4





Grupa: Zarejestrowani
Postów: 33
Pomógł: 5
Dołączył: 12.07.2010

Ostrzeżenie: (0%)
-----


Zamiast tego var_dump()
Go to the top of the page
+Quote Post
-Gość-
post
Post #5





Goście







Ten sam wynik mam co poprzednio
  1. <html>
  2. <head>
  3. <title>PHP Test</title>
  4. </head>
  5. <body>
  6. <?php
  7.  
  8. $apiKey = '6xx5';
  9. $url = 'http://apiwalutowe.pl/api/';
  10. $query = array();
  11. $query['key'] = $apiKey;
  12. $query['code'] = 'EURPLN';
  13. // tutaj ewentualnie inne parametry tabeli poniżej
  14.  
  15. $url = $url . '?' . http_build_query($query);
  16.  
  17. $ch = curl_init();
  18. curl_setopt($ch, CURLOPT_URL, $url);
  19. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  20. $json = curl_exec($ch);
  21.  
  22. $info = curl_getinfo($ch);
  23.  
  24. if ($info['code'] != 200)
  25. {
  26. die('BŁĄD: ' . $json['message']);
  27. }
  28.  
  29. $data = json_decode($json);
  30.  
  31. $data->rate;
  32. //echo $data->rate;//tez probowalem
  33.  
  34. //var_dump($data);
  35.  
  36. ?>
  37. </body>
  38. </html>


Możliwe, że nie do końca zrozumiałem, dlatego dałem kod.
Go to the top of the page
+Quote Post
--kacper.chrapa--
post
Post #6





Goście







echo $data->data->rate;

tak pobieznie patrzac co zwrocilo api: czyli obiekt, ktory zawiera wlasciwosc data bedaca obiektem z wlasciwoscia rate (IMG:style_emoticons/default/wink.gif)

jakbys zmienil na :
$przyslano = json_decode($json);

to bedzie kod brzmial:

echo $przyslano->data->rate;

pozdrawiam,
Kacper Chrapa
Go to the top of the page
+Quote Post
-Gość-
post
Post #7





Goście







O to chodziło, działa, dzięki;)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 21:34