October 8, 200917 yr I'm trying to get the "trackViewUrl" string from this piece of json: { "resultCount":1, "results": [ {"wrapperType":"track", "kind":"song", "artistId":471744, "collectionId":66092910, "trackId":66092553, "artistName":"Coldplay", "collectionName":"X & Y", "trackName":"Talk", "collectionCensoredName":"X & Y", "trackCensoredName":"Talk", "artistViewUrl":"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewArtist?id=471744&uo=4", "collectionViewUrl":"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=66092553&id=66092910&s=143441&uo=4", "trackViewUrl":"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=66092553&id=66092910&s=143441&uo=4", "previewUrl":"http://a1039.phobos.apple.com/us/r1000/008/Music/31/a5/52/mzi.nlxlmqcg.aac.p.m4p", "artworkUrl60":"http://a1.phobos.apple.com/us/r1000/017/Features/40/f0/b6/dj.vjdvmixl.60x60-50.jpg", "artworkUrl100":"http://a1.phobos.apple.com/us/r1000/017/Features/40/f0/b6/dj.vjdvmixl.100x100-75.jpg", "collectionPrice":9.99000, "trackPrice":1.29000, "collectionExplicitness":"notExplicit", "trackExplicitness":"notExplicit", "discCount":1, "discNumber":1, "trackCount":13, "trackNumber":5, "trackTimeMillis":311356, "country":"USA", "currency":"USD", "primaryGenreName":"Alternative"}] } Using this PHP Code: $request_url = 'http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch?term=talk+coldplay&media=music&limit=1'; $json = file_get_contents($request_url); $decode = json_decode($json, true); var_dump($decode); I have converted it into this PHP Array: array(2) { ["resultCount"]=> int(1) ["results"]=> array(1) { [0]=> array(28) { ["wrapperType"]=> string(5) "track" ["kind"]=> string(4) "song" ["artistId"]=> int(471744) ["collectionId"]=> int(66092910) ["trackId"]=> int(66092553) ["artistName"]=> string( "Coldplay" ["collectionName"]=> string(5) "X & Y" ["trackName"]=> string(4) "Talk" ["collectionCensoredName"]=> string(5) "X & Y" ["trackCensoredName"]=> string(4) "Talk" ["artistViewUrl"]=> string(75) "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewArtist?id=471744&uo=4" ["collectionViewUrl"]=> string(96) "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=66092553&id=66092910&s=143441&uo=4" ["trackViewUrl"]=> string(96) "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=66092553&id=66092910&s=143441&uo=4" ["previewUrl"]=> string(80) "http://a1039.phobos.apple.com/us/r1000/008/Music/31/a5/52/mzi.nlxlmqcg.aac.p.m4p" ["artworkUrl60"]=> string(82) "http://a1.phobos.apple.com/us/r1000/017/Features/40/f0/b6/dj.vjdvmixl.60x60-50.jpg" ["artworkUrl100"]=> string(84) "http://a1.phobos.apple.com/us/r1000/017/Features/40/f0/b6/dj.vjdvmixl.100x100-75.jpg" ["collectionPrice"]=> float(9.99) ["trackPrice"]=> float(1.29) ["collectionExplicitness"]=> string(11) "notExplicit" ["trackExplicitness"]=> string(11) "notExplicit" ["discCount"]=> int(1) ["discNumber"]=> int(1) ["trackCount"]=> int(13) ["trackNumber"]=> int(5) ["trackTimeMillis"]=> int(311356) ["country"]=> string(3) "USA" ["currency"]=> string(3) "USD" ["primaryGenreName"]=> string(11) "Alternative" } } } I can't work out what I need to get the value of the trackViewUrl string though I've tried: echo $decode["trackViewUrl"]; But it says it is undefined ? Can any see where I'm going wrong here..
October 8, 200917 yr There are actually 3 nested arrays here, the $decode array(2), the 'results' array(1) and another array(28) that holds the values you want. You'll access them like so: echo $decode['results'][0]['trackViewUrl']; You'll find it easier to read the output of var_dump by placing it inside <pre> tags echo '<pre>'; var_dump($decode); echo '</pre>';
October 8, 200917 yr Author Little bit more help needed I'm trying to implement an If Statement so that if the 'resultCount' String equals 0 (i.e No Results were returned) then it doesn't try to find the trackViewUrl string so it doesn't produce an error. $json = file_get_contents("http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch?term=".$cleanst."&limit=1"); $decode = json_decode($json, true); $resultcount = $decode['resultCount']; if($resultcount = '1') {$trackView = $decode['results'][0]['trackViewUrl']; echo "<a href='".$trackView."'>Download</a>";} else if ($resultcount = "0") {echo "Download Not Avalible";} This doesn't seem to work but I've check the $resultcount variable and it is outputting the correct amount of results for that query..
October 8, 200917 yr At the moment you're using the assignment operator '=' instead of comparing the values '=='. You're code should look like this: if($resultcount == 1) { $trackView = $decode['results'][0]['trackViewUrl']; echo "<a href='".$trackView."'>Download</a>"; } else if ($resultcount == 0) { echo "Download Not Available"; }
Create an account or sign in to comment