Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

2 Stron V   1 2 >  
Reply to this topicStart new topic
> [json][php] odczyt pliku json i wyświetlenie w php, pobieranie danych
socially
post 6.11.2016, 17:45:59
Post #1





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 11.10.2015

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


Witam!
Potrzebuje pomocy, mam taki plik php pobierający dane w formacje json
  1. //Przekazywania kryteriów wyszukiwania do adresu URL
  2. $url = sprintf("%s?%s", $url, http_build_query($data));
  3.  
  4.  
  5. //Ustawianie typu autoryzacji
  6. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  7. //Przekazywanie danych do logowania
  8. curl_setopt($curl, CURLOPT_USERPWD , "$appkey:$secret");
  9. //Przekazywania adresu URL z akcją
  10. curl_setopt($curl, CURLOPT_URL, $url);
  11. //Ustawienia dotyczące zwrotu z serwera
  12. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  13. //Pobieranie rezultatu
  14. $result = curl_exec($curl);
  15. //Wyświetlanie w formie JSON
  16. $result = curl_exec( $curl );
  17. curl_close( $curl );
  18. $return = json_decode($result, true);
  19.  
  20. print_r ($return);


pobrane dane wyświetlają się w przeglądarce w takim formacie


Array ( [code] => 200 [status] => success [number_of_elements] => 20981 [message] => [data] => Array ( [0] => Array ( [account] => 1.socially.smtp [email] => nikol.kidman@onet.pl [source] => smtp [reason] => hardbounce [comment] => host mx.poczta.onet.pl[213.180.147.146] said: 550 5.1.1 : Recipient address rejected: User unknown (in reply to RCPT TO command) [count] => 1 [created_at] => 2016-11-06 17:25:59 [updated_at] => 2016-11-06 17:25:59 [id] => 581f5a2d87e9c045edad10e0 ) ) [req_id] => 4FwisMbwda )


jak pobrać i wyświetlić tylko zawartość?
[email] => nikol.kidman@onet.pl

chcę aby na stronie wyświetlał się tylko adres @
nikol.kidman@onet.pl

Będę wdzięczny za pomoc

Ten post edytował socially 6.11.2016, 17:46:47
Go to the top of the page
+Quote Post
nospor
post 6.11.2016, 17:56:51
Post #2





Grupa: Moderatorzy
Postów: 36 556
Pomógł: 6314
Dołączył: 27.12.2004




Przeciez twoim wynikiem koncowym jest najzwyklejsza tablica.... manual->dzial tablice

Przenosze na przedszkole


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
viking
post 6.11.2016, 17:58:19
Post #3





Grupa: Zarejestrowani
Postów: 6 376
Pomógł: 1116
Dołączył: 30.08.2006

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


$return['message']['data'][0]['email']


--------------------
Go to the top of the page
+Quote Post
socially
post 6.11.2016, 19:03:16
Post #4





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 11.10.2015

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


Cytat(viking @ 6.11.2016, 17:58:19 ) *
$return['message']['data'][0]['email']


  1.  
  2. //Wyświetlanie
  3. $result = curl_exec( $curl );
  4. curl_close( $curl );
  5. $return = json_decode($result, true);
  6. print_r ($return);


może ktoś podpowie jak rozpisać kod php z tym ?
$return['message']['data'][0]['email']
bo niestety kombinuję i dalej albo mam podwójne tablice wyświetlane, albo tylko tekst array a niestety @ nie wyświetla

Ten post edytował socially 6.11.2016, 19:04:45
Go to the top of the page
+Quote Post
Tomplus
post 6.11.2016, 21:30:20
Post #5





Grupa: Zarejestrowani
Postów: 1 872
Pomógł: 230
Dołączył: 20.03.2005
Skąd: Będzin

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


Jeżeli wyświetla ci tekst array, to oznacza że dana wartość jest tablicą.
Spróbuj wydrukować wartość z print_r(), nalepiej krok po kroku:

najpierw $return['message']
potem $return['message']['data']
potem potem $return['message']['data'][0]

Szybko w ten sposób wykryjesz błąd dlaczego nie widzisz adresu email i prawdopodobnie innych danych.

Go to the top of the page
+Quote Post
socially
post 6.11.2016, 23:00:51
Post #6





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 11.10.2015

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


nieco zmieniłem skrypt i wygląda to teraz tak
  1. //Pobieranie rezultatu
  2. $result = curl_exec($curl);
  3. //Wyświetlanie w formie JSON
  4. echo $result;


wyświetla w przeglądarce wszsytkie dane

{"code":200,"status":"success","number_of_elements":20984,"message":null,"data":[{"account":"1.socially.smtp","email":"szczepanikd15072@gmail.com","source":"smtp","reason":"hardbounce","comment":"host gmail-smtp-in.l.google.com[66.102.1.27] said: 550-5.1.1 The email account that you tried to reach does not exist. Please try 550-5.1.1 double-checking the recipient's email address for typos or 550-5.1.1 unnecessary spaces. Learn more at 550 5.1.1 https:\/\/support.google.com\/mail\/?p=NoSuchUser vg1si26209372wjb.19 - gsmtp (in reply to RCPT TO command)","count":1,"created_at":"2016-11-06 22:49:32","updated_at":"2016-11-06 22:49:32","id":"581fa61c87e9c045edb1fc5e"}],"req_id":"4dAn1EhX5s"}

tylko teraz jak w kolejnej lini albo tylko wyświetlić?
szczepanikd15072@gmail.com

  1. //Pobieranie rezultatu
  2. $result = curl_exec($curl);
  3. //Wyświetlanie w formie JSON
  4. echo $result;
  5. i tu próbowałem wstawić już wszystko ale za nic nie chce wyświetlić samego email


próbowałem i na poprzedniej wersji

  1. $result = curl_exec( $curl );
  2. curl_close( $curl );
  3. $return = json_decode($result, true);
  4. print_r ($return['message']);


tylko biała strona i to samo przy
print_r ($return['message']['data'][0]['email']);

Ten post edytował socially 6.11.2016, 23:11:52
Go to the top of the page
+Quote Post
nospor
post 6.11.2016, 23:15:53
Post #7





Grupa: Moderatorzy
Postów: 36 556
Pomógł: 6314
Dołączył: 27.12.2004




$return = json_decode($result, true);
print_r ($return['data'][0]['email']);

Nawet nie zajrzales do manuala jak prosilem....


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
socially
post 7.11.2016, 01:38:22
Post #8





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 11.10.2015

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


Cytat(nospor @ 6.11.2016, 23:15:53 ) *
$return = json_decode($result, true);
print_r ($return['data'][0]['email']);

Nawet nie zajrzales do manuala jak prosilem....


Zajrzałem ale skupiłem się na innym rozwiązaniu i w końcu wszsytko działa jak powinno.

  1. $query = mysql_query("SELECT *
  2. FROM `vld_members`
  3. WHERE (`group_id` = 2) AND (`cron` = 0) LIMIT 1");
  4.  
  5. while($row = mysql_fetch_assoc($query)) {
  6.  
  7. $email = $row['email'];
  8.  
  9. }
  10. //Inicjalizacja biblioteki CURL
  11. $curl = curl_init();
  12. //Ustawienie adresu z którego zostaną pobrane dane
  13. $url = "https://testwyadreswww/$email";
  14. //Ustawienie klucza App Key
  15. $appkey = 'appkey ';
  16. //Ustawienie klucza Secret Key
  17. $secret = 'secret ';
  18. //Ustawianie typu autoryzacji
  19. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  20. //Przekazywanie danych do logowania
  21. curl_setopt($curl, CURLOPT_USERPWD , "$appkey:$secret");
  22. //Przekazywania adresu URL z akcją
  23. curl_setopt($curl, CURLOPT_URL, $url);
  24. //Ustawienia dotyczące zwrotu z serwera
  25. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  26. //Pobieranie rezultatu
  27. $result = curl_exec( $curl );
  28. curl_close( $curl );
  29. $return = json_decode($result, true);
  30. $code = ($return['code']);
  31.  
  32.  
  33. if($code == 200){
  34. echo $email;
  35. echo $code;
  36.  
  37. $db = mysql_query("SELECT * from vld_members WHERE email='".$text."'") or die (mysql_error());
  38. $array = mysql_fetch_assoc($db);
  39.  
  40. mysql_query("UPDATE vld_members SET spam = '1', cron = 1 WHERE email = '".$email."'") or die (mysql_error());
  41.  
  42. }
  43.  
  44. if($code == 404){
  45. echo $email;
  46. echo $code;
  47.  
  48. $db = mysql_query("SELECT * from vld_members WHERE email='".$text."'") or die (mysql_error());
  49. $array = mysql_fetch_assoc($db);
  50.  
  51. mysql_query("UPDATE vld_members SET cron = 1 WHERE email = '".$email."'") or die (mysql_error());
  52.  
  53. }
  54.  
  55. echo "OK";


Dziękuję wszystkim za pomoc
Go to the top of the page
+Quote Post
Ryrzy
post 21.10.2017, 08:13:34
Post #9





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 14.03.2014

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


Witam,
Pozwole sobie odkopać trochę ten temat smile.gif

Mam dane z jsona, między innymi jest tam lista graczy z serwera, wygląda to tak:
  1. [players] => Array
  2. (
  3. [0] => stdClass Object
  4. (
  5. [id] => 0
  6. [name] => XXX
  7. [score] => 0
  8. [time] => 3143.5852050781
  9. [gq_name] => XXX
  10. [gq_score] => 0
  11. [gq_time] => 3143.5852050781
  12. )
  13.  
  14. [1] => stdClass Object
  15. (
  16. [id] => 0
  17. [name] => YYY
  18. [score] => 0
  19. [time] => 3127.2993164062
  20. [gq_name] => YYY
  21. [gq_score] => 0
  22. [gq_time] => 3127.2993164062
  23. )
  24.  
  25. ITD

jak to poprawnie wyciągnąć do tabelki ?

  1. print_r ($return['players']);

wyciąga mi całą liste graczy

cały skrypt wyglada tak:
  1. <?php
  2. if(!function_exists("curl_init")) die("cURL nie jest zainstalowany");
  3.  
  4. $url = 'https://xx.yy/query';
  5. $curl = curl_init();
  6. curl_setopt( $curl, CURLOPT_URL, $url );
  7. curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
  8. $result = curl_exec( $curl );
  9. curl_close( $curl );
  10. $return = json_decode($result, true);
  11.  
  12. print_r ($return);
  13.  
  14. //testy
  15. echo "<br><br>";
  16. print_r ($return['players'][0]['name']);
  17. echo "<br><br>";
  18. print_r ($return['players'][0]);
  19. echo "<br><br>";
  20. print_r ($return['players']);
  21. echo "<br><br>";
  22. print_r ($return['world.size']);
  23. ?>


Ten post edytował Ryrzy 21.10.2017, 08:16:38
Go to the top of the page
+Quote Post
viking
post 21.10.2017, 08:21:53
Post #10





Grupa: Zarejestrowani
Postów: 6 376
Pomógł: 1116
Dołączył: 30.08.2006

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


Według danych które wkleiłeś masz zwróconą kolekcję obiektów.


--------------------
Go to the top of the page
+Quote Post
Ryrzy
post 21.10.2017, 08:36:57
Post #11





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 14.03.2014

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


zgadza sie, pobieram calosc, z tego sobie moge poszczególne info wyciągnąć jak np liste graczy (składnia wyzej)
Go to the top of the page
+Quote Post
viking
post 21.10.2017, 08:59:48
Post #12





Grupa: Zarejestrowani
Postów: 6 376
Pomógł: 1116
Dołączył: 30.08.2006

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


To jakie jest pytanie? Do jakiej tabelki? W bazie? Lecisz foreachem i wkładasz.


--------------------
Go to the top of the page
+Quote Post
Ryrzy
post 21.10.2017, 09:07:03
Post #13





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 14.03.2014

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


tabelki tabelki biggrin.gif nie do tabeli bazy smile.gif
chce na stronie utworzyć z tego tabelkę html 2 pozycje, nick i czas. 2 kolumny, x wierszy. pewnie wydaje się to banalne, ale jakoś tego nie potrafie przeniesc w kod wink.gif starego psa ciezko nauczyc nowych sztuczek wink.gif
Go to the top of the page
+Quote Post
viking
post 21.10.2017, 09:25:32
Post #14





Grupa: Zarejestrowani
Postów: 6 376
Pomógł: 1116
Dołączył: 30.08.2006

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


A co to za nowa sztuczka? Masz tablicę, wrzucasz ją do pętli i wyświetlasz dane. Kompletne podstawy PHP.
http://php.net/manual/en/control-structures.foreach.php


--------------------
Go to the top of the page
+Quote Post
gitbejbe
post 21.10.2017, 09:31:57
Post #15





Grupa: Zarejestrowani
Postów: 516
Pomógł: 63
Dołączył: 27.08.2012

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


  1.  
  2. <table>
  3.  
  4. <thead>
  5. <tr>
  6. <th>Nick</th>
  7. <th>Czas</th>
  8. </tr>
  9. </thead>
  10. <tbody>
  11. <?php
  12. foreach($players as &key => $value){
  13. echo '<tr><td>'.$value['name'].'</td><td>'.$value['time'].'</td></tr>';
  14. }
  15. ?>
  16. </tbody>
  17.  
  18. </table>
  19.  
Go to the top of the page
+Quote Post
Ryrzy
post 21.10.2017, 09:39:44
Post #16





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 14.03.2014

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


@viking no i widzisz, już mnie naprowadziłeś - foreach wink.gif
@gitbejbe coś jest nie hallo, mam po tym Error500 tongue.gif tak to wkleiłem:
  1. <?php
  2. if(!function_exists("curl_init")) die("cURL nie jest zainstalowany");
  3.  
  4. $url = 'xx.yy/query';
  5. $curl = curl_init();
  6. curl_setopt( $curl, CURLOPT_URL, $url );
  7. curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
  8. $result = curl_exec( $curl );
  9. curl_close( $curl );
  10. $return = json_decode($result, true);
  11.  
  12. print_r ($return);
  13. echo "<br><br>";
  14. print_r ($return['players'][0]['name']);
  15. echo "<br><br>";
  16. print_r ($return['players'][0]);
  17. echo "<br><br>";
  18. print_r ($return['players']);
  19. echo "<br><br>";
  20. print_r ($return['world.size']);
  21. ?>
  22. <html>
  23. <head>
  24. <meta charset="utf-8">
  25. <table>
  26.  
  27. <thead>
  28. <tr>
  29. <th>Nick</th>
  30. <th>Czas</th>
  31. </tr>
  32. </thead>
  33. <tbody>
  34. <?php
  35. foreach($players as &key => $value){
  36. echo "<tr><td>".$value['name']."</td><td>".$value['time']."</td></tr>";
  37. }
  38. ?>
  39. </tbody>
  40.  
  41. </table>
  42. </body>
  43. </html>


Ten post edytował Ryrzy 21.10.2017, 09:40:15
Go to the top of the page
+Quote Post
viking
post 21.10.2017, 09:52:39
Post #17





Grupa: Zarejestrowani
Postów: 6 376
Pomógł: 1116
Dołączył: 30.08.2006

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


Już cię w pierwszym poście naprowadziłem, później się musiałem powtórzyć wink.gif
Zapoznaj się z przypiętym tematem Temat: Jak poprawnie zadac pytanie oraz przygotowac srodowisko pracy zwłaszcza raportowanie błędów.


--------------------
Go to the top of the page
+Quote Post
gitbejbe
post 21.10.2017, 10:25:26
Post #18





Grupa: Zarejestrowani
Postów: 516
Pomógł: 63
Dołączył: 27.08.2012

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


Napisałem Ci gotowca, faktycznie jest błąd ale serio nie potrafisz go zweryfikować ? Jakim cudem napisałeś resztę kodu ?

zmień całego foreacha na:

  1. foreach($return['players'] as $key => $value){
  2. echo "<tr><td>".$value->name."</td><td>".$value->time."</td></tr>";
  3. }


Ten post edytował gitbejbe 21.10.2017, 10:29:25
Go to the top of the page
+Quote Post
Ryrzy
post 21.10.2017, 10:43:23
Post #19





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 14.03.2014

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


ctrl C, ctrl V + wlasne obserwacje... tak to u mnie dziala biggrin.gif jak już załapie jak coś działa to jakoś wchodzi do glowy, żadna teoria, praktyka i testowanie...

btw. zwraca pustą tabele...
Go to the top of the page
+Quote Post
viking
post 21.10.2017, 11:22:29
Post #20





Grupa: Zarejestrowani
Postów: 6 376
Pomógł: 1116
Dołączył: 30.08.2006

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


Tak to sobie można w budowlance robić. Położymy ten kabel 0.5 m obok i tez będzie ok. W programowaniu dostajesz konkretny błąd i masz go poprawić żeby działało dalej. Dlatego podstawą jest ich czytanie.


--------------------
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 4.05.2025 - 12:19