Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Odczyt JSON
Forum PHP.pl > Forum > Przedszkole
Dzyydz
Witam.

Mam problem z odczytaniem pliku zakodowanym w JSON. Chcę użyć geocoding'u do odczytania współrzędnych danej miejscowości. Jednak po zastosowaniu moich sposobów nic mi nie wyświetla. Oto jak to staram się uczynić.

  1. <?php
  2.  
  3. $input = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false");
  4. $start = strpos($input,'(')+1;
  5. $length = strrpos($input,')') - $start;
  6.  
  7. $json = substr($input,$start,$length);
  8. $output = json_decode($json,true);
  9. print_r($output);
  10.  
  11. ?>


Próbowałem wcześniej innymi sposobami ale nie wychodziły więc znalazłem podobny kod do tego wyżej no ale on również mi nie działa.
Czy jest mi ktoś wstanie pomóc ? :]

Pozdrawiam!
lobopol
  1. $input = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false");
  2. $result = json_decode($input, true);
  3. print_r($result);

Strona zwraca jsona więc bezsensu jakieś podciągi starasz się wyciągnąć, rozwalasz sobie strukturę jsona.
Dzyydz
Dzięki. Teraz jest ok.

Mam jeszcze jedno pytanie:

Mam taki kod JSON:

  1. {
  2. "debug_info" : [],
  3. "html_attributions" : [],
  4. "result" : {
  5. "address_components" : [
  6. {
  7. "long_name" : "48",
  8. "short_name" : "48",
  9. "types" : [ "street_number" ]
  10. },
  11. {
  12. "long_name" : "Pirrama Rd",
  13. "short_name" : "Pirrama Rd",
  14. "types" : [ "route" ]
  15. },
  16. {
  17. "long_name" : "Pyrmont",
  18. "short_name" : "Pyrmont",
  19. "types" : [ "locality", "political" ]
  20. },
  21. {
  22. "long_name" : "Nsw",
  23. "short_name" : "Nsw",
  24. "types" : [ "administrative_area_level_1", "political" ]
  25. },
  26. {
  27. "long_name" : "AU",
  28. "short_name" : "AU",
  29. "types" : [ "country", "political" ]
  30. },
  31. {
  32. "long_name" : "2009",
  33. "short_name" : "2009",
  34. "types" : [ "postal_code" ]
  35. }
  36. ],
  37. "formatted_address" : "5/48 Pirrama Rd, Pyrmont Nsw, Australia",
  38. "formatted_phone_number" : "(02) 9374 4000",
  39. "geometry" : {
  40. "location" : {
  41. "lat" : -33.866933,
  42. "lng" : 151.195791
  43. }
  44. },


Nie piszę wiecej kodu JSON.
Chcę teraz pobrać "long_name" : "Pyrmont",
Próbowałem pisać coś takiego:
  1. $fullurl3 = "https://maps.googleapis.com/maps/api/place/details/json?reference=".$reference."&sensor=true&key=AIzaSyCWLmxpB756v-S2RYyYnJUMMXWgftQ3oHk";
  2.  
  3. $string3 .= file_get_contents($fullurl3); // get json content
  4. $json_a3 = json_decode($string3, true); //json decoder
  5.  
  6. $namecity = $json_a3['results']['address_components'][3]['long_name'];
  7. echo $namecity;


No ale nie wyświetla mi nic. Ktoś może pomóc ?

Pozdrawiam.
Dzyydz
Znalazłem błąd. Dzięki.

Temat do zamknięcia.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.