JSON einen Wert ausgeben

  • PHP

Es gibt 10 Antworten in diesem Thema. Der letzte Beitrag () ist von Tobias17.

    JSON einen Wert ausgeben

    Hay Leute, nach Stundenlangen recherchieren und ausprobieren bin ich mit meinem Latein am Ende.

    Ich habe folgende Webseite:
    tm-computertech.de/104.6/api.php

    ["name"]=> string(20) "I HATE U, I LOVE YOU" ["artistName"]=> string(26) "GNASH FEAT. OLIVIA O'BRIEN"

    Das Problem ist, ich möchte NUR name oder NUR artistName ausgeben(je nachdem wie ich es per GET übergebe)

    Sprich: api.php?string=name
    api.php?string=artistName

    Leider funktioniert das überhaupt nicht
    Ich bekomme per dump_var($variable); nur den Wert NULL

    Ursprünglich versuche ich das ganze von folgender Webseite abzurufen:

    np.radioplayer.de/qp/v3/onair?…e=200&descriptionSize=200

    Meinen Code wollt ihr garnicht sehen :D

    Wer ihn trotzdem möchte:

    PHP-Quellcode

    1. <?php
    2. $jsonfile = file_get_contents('http://np.radioplayer.de/qp/v3/onair?rpIds=25&nameSize=200&artistNameSize=200&descriptionSize=200');
    3. $jsonfile = jsonp_decode($jsonfile, false);
    4. var_dump($jsonfile);
    5. $count = $jsonfile[1];
    6. echo $jsonfile[2][$count-1][3];
    7. function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter to json_decode
    8. if($jsonp[0] !== '[' && $jsonp[0] !== '{') { // we have JSONP
    9. $jsonp = substr($jsonp, strpos($jsonp, '('));
    10. }
    11. return (array)json_decode(trim($jsonp,'();'), $assoc);
    12. }
    13. ?>


    Ich muss dazu sagen ich kann überhaupt kein JSON abee brauche diese Daten :(
    Wenn du dir das JSON genauer anschaust, sollte das Abfragen mit PHP kein Problem mehr sein.
    JSON

    Quellcode

    1. {
    2. "responseStatus" : "SUCCESS",
    3. "total" : 2,
    4. "results" : {
    5. "25" : [
    6. {
    7. "type" : "SI",
    8. "rpId" : "25",
    9. "serviceName" : "...",
    10. "name" : "",
    11. "artistName" : "",
    12. "song" : false,
    13. "description" : "...",
    14. "imageUrl" : "...",
    15. "startTime" : "",
    16. "stopTime" : ""
    17. },
    18. {
    19. "type" : "PE_E",
    20. "rpId" : "25",
    21. "serviceName" : "...",
    22. "name" : "...",
    23. "artistName" : "...",
    24. "song" : true,
    25. "description" : "",
    26. "imageUrl" : "...",
    27. "startTime" : "1477138046",
    28. "stopTime" : "1477138250"
    29. }
    30. ]
    31. }
    32. }


    PHP-Quellcode

    1. if($jsonfile['responseStatus'] === 'SUCCESS') {
    2. $results = $jsonfile['results']['25']
    3. $result = $results[count($results) - 1]
    4. $name = $result['name']
    5. echo $name;
    6. }


    Wobei du dir natürlich genau anschauen musst, ob die Daten immer so zur Verfügung gestellt werden.
    Idealerweise überprüfst du auch immer mit isset oder ggf. besser mit array_key_exists ob das JSON so ist, wie du es erwartet hast.
    Hay!

    Danke dir erstmal!

    Leider bleibt die Seite weiterhin weiss.

    Liegt das eventuell am

    PHP-Quellcode

    1. $jsonfile = file_get_contents('http://np.radioplayer.de/qp/v3/onair?rpIds=25&nameSize=200&artistNameSize=200&descriptionSize=200');
    ?



    Habe jetzt noch das Callback am Anfang entfernt inkl der Klammern und jetzt sagt er mir bei einem Generator dass der JSON Valide ist aber dennoch bleibt die Seite weiss.

    Mein Code sieht jetzt so aus:

    PHP-Quellcode

    1. $jsonfile = file_get_contents('http://np.radioplayer.de/qp/v3/onair?rpIds=25&nameSize=200&artistNameSize=200&descriptionSize=200');
    2. $jsonfile = substr($jsonfile, 9, strlen($jsonfile) - 10);
    3. if($jsonfile['responseStatus'] === 'SUCCESS') {
    4. $results = $jsonfile['results']['25'];
    5. $result = $results[count($results) - 1];
    6. $name = $result['name'];
    7. echo $name;
    8. }



    edit//

    er zeigt mir an:

    PHP-Quellcode

    1. ​Warning: Illegal string offset 'responseStatus' in /var/www/virtual/tm-computertech.de/htdocs/104.6/api.php on line 5
    dann bekomme ich solch ein WirrWarr:

    PHP-Quellcode

    1. ​array(3) { ["responseStatus"]=> string(7) "SUCCESS" ["total"]=> int(2) ["results"]=> array(1) { [25]=> array(2) { [0]=> array(10) { ["type"]=> string(2) "SI" ["rpId"]=> string(2) "25" ["serviceName"]=> string(27) "104.6 RTL Berlins Hit-Radio" ["name"]=> string(0) "" ["artistName"]=> string(0) "" ["song"]=> bool(false) ["description"]=> string(96) "Mehr Abwechslung und weniger Wiederholungen mit den besten neuen Hits und den RTL Greatest Hits." ["imageUrl"]=> string(81) "http://derpimagecache.s3.amazonaws.com/image/25_86x48_2014-12-05-10-30-00-338.png" ["startTime"]=> string(0) "" ["stopTime"]=> string(0) "" } [1]=> array(10) { ["type"]=> string(4) "PE_E" ["rpId"]=> string(2) "25" ["serviceName"]=> string(27) "104.6 RTL Berlins Hit-Radio" ["name"]=> string(10) "BE THE ONE" ["artistName"]=> string(8) "DUA LIPA" ["song"]=> bool(true) ["description"]=> string(0) "" ["imageUrl"]=> string(81) "http://derpimagecache.s3.amazonaws.com/image/25_86x48_2014-12-05-10-30-00-338.png" ["startTime"]=> string(10) "1477142193" ["stopTime"]=> string(10) "1477142381" } } }


    Verwendet habe ich:

    PHP-Quellcode

    1. ​var_dump(json_decode($jsonfile, true));


    ohne var_dump

    PHP-Quellcode

    1. ​$jsonfile = json_decode($jsonfile, true);


    sagt er:

    PHP-Quellcode

    1. "{\"responseStatus\":\"SUCCESS\",\"total\":2,\"results\":{\"25\":[{\"type\":\"SI\",\"rpId\":\"25\",\"serviceName\":\"104.6 RTL Berlins Hit-Radio\",\"name\":\"\",\"artistName\":\"\",\"song\":false,\"description\":\"Mehr Abwechslung und weniger Wiederholungen mit den besten neuen Hits und den RTL Greatest Hits.\",\"imageUrl\":\"http:\/\/derpimagecache.s3.amazonaws.com\/image\/25_86x48_2014-12-05-10-30-00-338.png\",\"startTime\":\"\",\"stopTime\":\"\"},{\"type\":\"PE_E\",\"rpId\":\"25\",\"serviceName\":\"104.6 RTL Berlins Hit-Radio\",\"name\":\"CLOSER\",\"artistName\":\"THE CHAINSMOKERS FEAT. HALSEY\",\"song\":true,\"description\":\"\",\"imageUrl\":\"http:\/\/derpimagecache.s3.amazonaws.com\/image\/25_86x48_2014-12-05-10-30-00-338.png\",\"startTime\":\"1477141949\",\"stopTime\":\"1477142155\"}]}}"
    mache ich ja trotzdem bekomme ich nicht den Wert den ich haben will sondern nur den Fehler bzw die Warnung:

    PHP-Quellcode

    1. ​Warning: Illegal string offset 'responseStatus' in /var/www/virtual/tm-computertech.de/htdocs/104.6/api.php on line 9


    und ich weiss einfach nicht wieso.

    Das ganze wenn ich es enkodiere sieht auch nicht so schön aufgelistet bei mir aus siehe:

    tm-computertech.de/104.6/api.php
    Danke erstmal, habe jetzt folgenden Code:

    PHP-Quellcode

    1. <?php
    2. $jsonfile = file_get_contents('http://np.radioplayer.de/qp/v3/onair?rpIds=25&nameSize=200&artistNameSize=200&descriptionSize=200');
    3. $jsonfile = jsonp_decode($jsonfile, true);
    4. if($jsonfile['responseStatus'] === 'SUCCESS') {
    5. $results = $jsonfile['results'][25];
    6. $result = $results[count($results) - 1];
    7. $name = $result['name'];
    8. echo $name;
    9. }
    10. function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter to json_decode
    11. if($jsonp[0] !== '[' && $jsonp[0] !== '{') { // we have JSONP
    12. $jsonp = substr($jsonp, strpos($jsonp, '('));
    13. }
    14. return (array)json_decode(trim($jsonp,'();'), $assoc);
    15. }
    16. ?>


    keine Fehlermeldung mehr aber Seite bleibt weiss siehe

    tm-computertech.de/104.6/api.php

    //edit

    muss dazu sagen dass es manchmal 2 einträge sind.
    sollten es 2 einträge sein, so ist der erste eintrag immer leer

    JAPP!! Mit description gehts!!

    Danke euch!!!

    wie kriege ich jetzt aber hin dass wenn 2 einträge sind, er den eintrag nimmt wo song auf true ist?

    Danke für eure Geduld!!!!