2014-03-26 116 views
0

我想從php下的json獲得每首歌的標題。我無法獲得所有的名字。我嘗試了下面的代碼,但它沒有顯示任何內容。我非常感謝任何幫助。提前致謝。無法獲得json_decode字段

代碼:

$json = file_get_contents('http://itunes.apple.com/us/rss/topsongs/limit=5/json'); 
$data=(json_decode($json, true)); 

foreach ($data as $item) 
{ 
    foreach ($item->entry as $asset) 
    { 
     echo $asset->title; 
    } 

} 

JSON:

{ 
    "feed": { 
    "author": { 
     "name": { 
     "label": "iTunes Store" 
     }, 
     "uri": { 
     "label": "http://www.apple.com/itunes/" 
     } 
    }, 
    "entry": [ 
     { 
     "im:name": { 
      "label": "Happy (From \"Despicable Me 2\")" 
     }, 
     "im:image": [ 
      { 
      "label": "http://a923.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.55x55-70.jpg", 
      "attributes": { 
       "height": "55" 
      } 
      }, 
      { 
      "label": "http://a1645.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.60x60-50.jpg", 
      "attributes": { 
       "height": "60" 
      } 
      }, 
      { 
      "label": "http://a1228.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.170x170-75.jpg", 
      "attributes": { 
       "height": "170" 
      } 
      } 
     ], 
     "im:collection": { 
      "im:name": { 
      "label": "G I R L" 
      }, 
      "link": { 
      "attributes": { 
       "rel": "alternate", 
       "type": "text/html", 
       "href": "https://itunes.apple.com/us/album/g-i-r-l/id823593445?uo=2" 
      } 
      }, 
      "im:contentType": { 
      "im:contentType": { 
       "attributes": { 
       "term": "Album", 
       "label": "Album" 
       } 
      }, 
      "attributes": { 
       "term": "Music", 
       "label": "Music" 
      } 
      } 
     }, 
     "im:price": { 
      "label": "$1.29", 
      "attributes": { 
      "amount": "1.29000", 
      "currency": "USD" 
      } 
     }, 
     "im:contentType": { 
      "im:contentType": { 
      "attributes": { 
       "term": "Track", 
       "label": "Track" 
      } 
      }, 
      "attributes": { 
      "term": "Music", 
      "label": "Music" 
      } 
     }, 
     "rights": { 
      "label": "℗ 2014 Columbia Records, a Division of Sony Music Entertainment, 2013 Back Lot Music, under exclusive license to Columbia Records, a Division of Sony Music Entertainment" 
     }, 
     "title": { 
      "label": "Happy (From \"Despicable Me 2\") - Pharrell Williams" 
     }, 
     "link": [ 
      { 
      "attributes": { 
       "rel": "alternate", 
       "type": "text/html", 
       "href": "https://itunes.apple.com/us/album/happy-from-despicable-me-2/id823593445?i=823593456&uo=2" 
      } 
      }, 
      { 
      "im:duration": { 
       "label": "30000" 
      }, 
      "attributes": { 
       "title": "Preview", 
       "rel": "enclosure", 
       "type": "audio/x-m4a", 
       "href": "http://a1083.phobos.apple.com/us/r1000/014/Music/v4/4e/44/b7/4e44b7dc-aaa2-c63b-fb38-88e1635b5b29/mzaf_1844128138535731917.plus.aac.p.m4a", 
       "im:assetType": "preview" 
      } 
      } 
     ], 
     "id": { 
      "label": "https://itunes.apple.com/us/album/happy-from-despicable-me-2/id823593445?i=823593456&uo=2", 
      "attributes": { 
      "im:id": "823593456" 
      } 
     }, 
     "im:artist": { 
      "label": "Pharrell Williams", 
      "attributes": { 
      "href": "https://itunes.apple.com/us/artist/pharrell-williams/id14934728?uo=2" 
      } 
     }, 
     "category": { 
      "attributes": { 
      "im:id": "14", 
      "term": "Pop", 
      "scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2", 
      "label": "Pop" 
      } 
     }, 
     "im:releaseDate": { 
      "label": "2014-03-03T00:00:00-07:00", 
      "attributes": { 
      "label": "March 3, 2014" 
      } 
     } 
     }, 
     { 
     "im:name": { 
      "label": "All of Me" 
     }, 
     "im:image": [ 
      { 
      "label": "http://a1221.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.55x55-70.jpg", 
      "attributes": { 
       "height": "55" 
      } 
      }, 
      { 
      "label": "http://a1943.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.60x60-50.jpg", 
      "attributes": { 
       "height": "60" 
      } 
      }, 
      { 
      "label": "http://a982.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.170x170-75.jpg", 
      "attributes": { 
       "height": "170" 
      } 
      } 
     ], 
     "im:collection": { 
      "im:name": { 
      "label": "Love in the Future (Deluxe Edition)" 
      }, 
      "link": { 
      "attributes": { 
       "rel": "alternate", 
       "type": "text/html", 
       "href": "https://itunes.apple.com/us/album/love-in-future-deluxe-edition/id679297685?uo=2" 
      } 
      }, 
      "im:contentType": { 
      "im:contentType": { 
       "attributes": { 
       "term": "Album", 
       "label": "Album" 
       } 
      }, 
      "attributes": { 
       "term": "Music", 
       "label": "Music" 
      } 
      } 
     }, 
     "im:price": { 
      "label": "$1.29", 
      "attributes": { 
      "amount": "1.29000", 
      "currency": "USD" 
      } 
     }, 
     "im:contentType": { 
      "im:contentType": { 
      "attributes": { 
       "term": "Track", 
       "label": "Track" 
      } 
      }, 
      "attributes": { 
      "term": "Music", 
      "label": "Music" 
      } 
     }, 
     "rights": { 
      "label": "℗ 2013 Getting Out Our Dreams and Columbia Records, a Division of Sony Music Entertainment" 
     }, 
     "title": { 
      "label": "All of Me - John Legend" 
     }, 
     "link": [ 
      { 
      "attributes": { 
       "rel": "alternate", 
       "type": "text/html", 
       "href": "https://itunes.apple.com/us/album/all-of-me/id679297685?i=679297849&uo=2" 
      } 
      }, 
      { 
      "im:duration": { 
       "label": "30000" 
      }, 
      "attributes": { 
       "title": "Preview", 
       "rel": "enclosure", 
       "type": "audio/x-m4a", 
       "href": "http://a355.phobos.apple.com/us/r1000/041/Music4/v4/90/cf/04/90cf0482-07c0-fca0-549f-c1ea62c4bdef/mzaf_6715619947923767616.plus.aac.p.m4a", 
       "im:assetType": "preview" 
      } 
      } 
     ], 
     "id": { 
      "label": "https://itunes.apple.com/us/album/all-of-me/id679297685?i=679297849&uo=2", 
      "attributes": { 
      "im:id": "679297849" 
      } 
     }, 
     "im:artist": { 
      "label": "John Legend", 
      "attributes": { 
      "href": "https://itunes.apple.com/us/artist/john-legend/id16586443?uo=2" 
      } 
     }, 
     "category": { 
      "attributes": { 
      "im:id": "15", 
      "term": "R&B/Soul", 
      "scheme": "https://itunes.apple.com/us/genre/music-r-b-soul/id15?uo=2", 
      "label": "R&B/Soul" 
      } 
     }, 
     "im:releaseDate": { 
      "label": "2013-08-30T00:00:00-07:00", 
      "attributes": { 
      "label": "August 30, 2013" 
      } 
     } 
     }, 
     { 
     "im:name": { 
      "label": "Let It Go" 
     }, 
     "im:image": [ 
      { 
      "label": "http://a261.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.55x55-70.jpg", 
      "attributes": { 
       "height": "55" 
      } 
      }, 
      { 
      "label": "http://a983.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.60x60-50.jpg", 
      "attributes": { 
       "height": "60" 
      } 
      }, 
      { 
      "label": "http://a262.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.170x170-75.jpg", 
      "attributes": { 
       "height": "170" 
      } 
      } 
     ], 
     "im:collection": { 
      "im:name": { 
      "label": "Frozen (Deluxe Edition) [Original Motion Picture Soundtrack]" 
      }, 
      "link": { 
      "attributes": { 
       "rel": "alternate", 
       "type": "text/html", 
       "href": "https://itunes.apple.com/us/album/frozen-deluxe-edition-original/id728903889?uo=2" 
      } 
      }, 
      "im:contentType": { 
      "im:contentType": { 
       "attributes": { 
       "term": "Album", 
       "label": "Album" 
       } 
      }, 
      "attributes": { 
       "term": "Music", 
       "label": "Music" 
      } 
      } 
     }, 
     "im:price": { 
      "label": "$1.29", 
      "attributes": { 
      "amount": "1.29000", 
      "currency": "USD" 
      } 
     }, 
     "im:contentType": { 
      "im:contentType": { 
      "attributes": { 
       "term": "Track", 
       "label": "Track" 
      } 
      }, 
      "attributes": { 
      "term": "Music", 
      "label": "Music" 
      } 
     }, 
     "rights": { 
      "label": "℗ 2013 Walt Disney Records" 
     }, 
     "title": { 
      "label": "Let It Go - Idina Menzel" 
     }, 
     "link": [ 
      { 
      "attributes": { 
       "rel": "alternate", 
       "type": "text/html", 
       "href": "https://itunes.apple.com/us/album/let-it-go/id728903889?i=728904000&uo=2" 
      } 
      }, 
      { 
      "im:duration": { 
       "label": "30000" 
      }, 
      "attributes": { 
       "title": "Preview", 
       "rel": "enclosure", 
       "type": "audio/x-m4a", 
       "href": "http://a878.phobos.apple.com/us/r1000/023/Music/v4/0a/eb/c2/0aebc299-44e3-3465-3cdf-5bab336b547a/mzaf_7970981567593232494.plus.aac.p.m4a", 
       "im:assetType": "preview" 
      } 
      } 
     ], 
     "id": { 
      "label": "https://itunes.apple.com/us/album/let-it-go/id728903889?i=728904000&uo=2", 
      "attributes": { 
      "im:id": "728904000" 
      } 
     }, 
     "im:artist": { 
      "label": "Idina Menzel", 
      "attributes": { 
      "href": "https://itunes.apple.com/us/artist/idina-menzel/id3297504?uo=2" 
      } 
     }, 
     "category": { 
      "attributes": { 
      "im:id": "16", 
      "term": "Soundtrack", 
      "scheme": "https://itunes.apple.com/us/genre/music-soundtrack/id16?uo=2", 
      "label": "Soundtrack" 
      } 
     }, 
     "im:releaseDate": { 
      "label": "2013-11-25T00:00:00-07:00", 
      "attributes": { 
      "label": "November 25, 2013" 
      } 
     } 
     }, 
     { 
     "im:name": { 
      "label": "Dark Horse (feat. Juicy J)" 
     }, 
     "im:image": [ 
      { 
      "label": "http://a1657.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.55x55-70.jpg", 
      "attributes": { 
       "height": "55" 
      } 
      }, 
      { 
      "label": "http://a731.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.60x60-50.jpg", 
      "attributes": { 
       "height": "60" 
      } 
      }, 
      { 
      "label": "http://a1498.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.170x170-75.jpg", 
      "attributes": { 
       "height": "170" 
      } 
      } 
     ], 
     "im:collection": { 
      "im:name": { 
      "label": "PRISM (Deluxe Version)" 
      }, 
      "link": { 
      "attributes": { 
       "rel": "alternate", 
       "type": "text/html", 
       "href": "https://itunes.apple.com/us/album/prism-deluxe-version/id690928033?uo=2" 
      } 
      }, 
      "im:contentType": { 
      "im:contentType": { 
       "attributes": { 
       "term": "Album", 
       "label": "Album" 
       } 
      }, 
      "attributes": { 
       "term": "Music", 
       "label": "Music" 
      } 
      } 
     }, 
     "im:price": { 
      "label": "$1.29", 
      "attributes": { 
      "amount": "1.29000", 
      "currency": "USD" 
      } 
     }, 
     "im:contentType": { 
      "im:contentType": { 
      "attributes": { 
       "term": "Track", 
       "label": "Track" 
      } 
      }, 
      "attributes": { 
      "term": "Music", 
      "label": "Music" 
      } 
     }, 
     "rights": { 
      "label": "℗ 2013 Capitol Records, LLC" 
     }, 
     "title": { 
      "label": "Dark Horse (feat. Juicy J) - Katy Perry" 
     }, 
     "link": [ 
      { 
      "attributes": { 
       "rel": "alternate", 
       "type": "text/html", 
       "href": "https://itunes.apple.com/us/album/dark-horse-feat.-juicy-j/id690928033?i=690928460&uo=2" 
      } 
      }, 
      { 
      "im:duration": { 
       "label": "30000" 
      }, 
      "attributes": { 
       "title": "Preview", 
       "rel": "enclosure", 
       "type": "audio/x-m4a", 
       "href": "http://a1930.phobos.apple.com/us/r1000/022/Music/v4/a4/ac/63/a4ac6341-59e3-bcfe-b7f5-370100eea063/mzaf_4835051646631223146.plus.aac.p.m4a", 
       "im:assetType": "preview" 
      } 
      } 
     ], 
     "id": { 
      "label": "https://itunes.apple.com/us/album/dark-horse-feat.-juicy-j/id690928033?i=690928460&uo=2", 
      "attributes": { 
      "im:id": "690928460" 
      } 
     }, 
     "im:artist": { 
      "label": "Katy Perry", 
      "attributes": { 
      "href": "https://itunes.apple.com/us/artist/katy-perry/id64387566?uo=2" 
      } 
     }, 
     "category": { 
      "attributes": { 
      "im:id": "14", 
      "term": "Pop", 
      "scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2", 
      "label": "Pop" 
      } 
     }, 
     "im:releaseDate": { 
      "label": "2013-10-22T00:00:00-07:00", 
      "attributes": { 
      "label": "October 22, 2013" 
      } 
     } 
     }, 
     { 
     "im:name": { 
      "label": "The Man" 
     }, 
     "im:image": [ 
      { 
      "label": "http://a1169.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.55x55-70.jpg", 
      "attributes": { 
       "height": "55" 
      } 
      }, 
      { 
      "label": "http://a243.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.60x60-50.jpg", 
      "attributes": { 
       "height": "60" 
      } 
      }, 
      { 
      "label": "http://a1234.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.170x170-75.jpg", 
      "attributes": { 
       "height": "170" 
      } 
      } 
     ], 
     "im:collection": { 
      "im:name": { 
      "label": "Lift Your Spirit" 
      }, 
      "link": { 
      "attributes": { 
       "rel": "alternate", 
       "type": "text/html", 
       "href": "https://itunes.apple.com/us/album/lift-your-spirit/id798928288?uo=2" 
      } 
      }, 
      "im:contentType": { 
      "im:contentType": { 
       "attributes": { 
       "term": "Album", 
       "label": "Album" 
       } 
      }, 
      "attributes": { 
       "term": "Music", 
       "label": "Music" 
      } 
      } 
     }, 
     "im:price": { 
      "label": "$1.29", 
      "attributes": { 
      "amount": "1.29000", 
      "currency": "USD" 
      } 
     }, 
     "im:contentType": { 
      "im:contentType": { 
      "attributes": { 
       "term": "Track", 
       "label": "Track" 
      } 
      }, 
      "attributes": { 
      "term": "Music", 
      "label": "Music" 
      } 
     }, 
     "rights": { 
      "label": "℗ 2014 Aloe Blacc Recording, Inc. under exclusive license to XIX Recordings LLC/Interscope Records" 
     }, 
     "title": { 
      "label": "The Man - Aloe Blacc" 
     }, 
     "link": [ 
      { 
      "attributes": { 
       "rel": "alternate", 
       "type": "text/html", 
       "href": "https://itunes.apple.com/us/album/the-man/id798928288?i=798928362&uo=2" 
      } 
      }, 
      { 
      "im:duration": { 
       "label": "30000" 
      }, 
      "attributes": { 
       "title": "Preview", 
       "rel": "enclosure", 
       "type": "audio/x-m4a", 
       "href": "http://a85.phobos.apple.com/us/r1000/033/Music6/v4/8a/09/dc/8a09dcf0-4845-81ae-6936-a94972ad94e7/mzaf_5332424013433716082.plus.aac.p.m4a", 
       "im:assetType": "preview" 
      } 
      } 
     ], 
     "id": { 
      "label": "https://itunes.apple.com/us/album/the-man/id798928288?i=798928362&uo=2", 
      "attributes": { 
      "im:id": "798928362" 
      } 
     }, 
     "im:artist": { 
      "label": "Aloe Blacc", 
      "attributes": { 
      "href": "https://itunes.apple.com/us/artist/aloe-blacc/id4750752?uo=2" 
      } 
     }, 
     "category": { 
      "attributes": { 
      "im:id": "14", 
      "term": "Pop", 
      "scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2", 
      "label": "Pop" 
      } 
     }, 
     "im:releaseDate": { 
      "label": "2014-03-11T00:00:00-07:00", 
      "attributes": { 
      "label": "March 11, 2014" 
      } 
     } 
     } 
    ], 
    "updated": { 
     "label": "2014-03-26T14:09:41-07:00" 
    }, 
    "rights": { 
     "label": "Copyright 2008 Apple Inc." 
    }, 
    "title": { 
     "label": "iTunes Store: Top Songs" 
    }, 
    "icon": { 
     "label": "http://itunes.apple.com/favicon.ico" 
    }, 
    "link": [ 
     { 
     "attributes": { 
      "rel": "alternate", 
      "type": "text/html", 
      "href": "https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?cc=us&id=38&popId=1" 
     } 
     }, 
     { 
     "attributes": { 
      "rel": "self", 
      "href": "http://itunes.apple.com/us/rss/topsongs/limit=5/json" 
     } 
     } 
    ], 
    "id": { 
     "label": "http://itunes.apple.com/us/rss/topsongs/limit=5/json" 
    } 
    } 
} 
+1

變化'json_decode($ JSON,真)''到json_decode($ JSON)'。如果第二個參數設置爲true,則解碼輸出將是關聯數組,否則爲對象。 – barell

+0

我試過這也不工作。請嘗試上面的代碼它不會顯示任何東西。 – jason

+1

那你有什麼錯誤嗎?把'error_reporting(E_ALL); ini_set('display_errors',1);'在PHP腳本的頂部。 – barell

回答

1
$json = file_get_contents('http://itunes.apple.com/us/rss/topsongs/limit=5/json'); 
$data=json_decode($json); 

$titles = array(); 
foreach ($data as $item) 
{ 
    foreach ($item->entry as $asset) 
    { 
     $titles[] = $asset->title->label; 
    } 
} 
+0

真棒。感謝很多。 – jason