Search code examples
phpitunes

How to get the ID from itunes url?


I need to get the item ID from the iTunes url and it used to work like this before itunes changed there url structure;

$musicid = 'https://itunes.apple.com/album/the-endless-bridge-single/id1146508518?uo=1&v0=9989';
$musicid=explode('/id',$musicid);
$musicid=explode('?',$musicid[1]);
echo $musicid[0];

But now iTunes has deleted the 'id' prefix in the url so the above code does not return the id anymore, does anyone know a solution?

old itunes url; https://itunes.apple.com/album/the-endless-bridge-single/id1146508518?uo=1&v0=9989

new itunes url; https://itunes.apple.com/album/the-endless-bridge-single/1146508518?uo=1&v0=9989


Solution

  • You would just explode on the fourth slash, grabbing the fifth segment.

    Simply remove id from /id (so that you explode() on /), and check the sixth index instead of the second (with [5] instead of [1]):

    <?php
    
    $musicid = 'https://itunes.apple.com/album/the-endless-bridge-single/1146508518?uo=1&v0=9989';
    $musicid = explode('/', $musicid);
    $musicid = explode('?', $musicid[5]);
    echo $musicid[0]; // 1146508518
    

    This can be seen working here.

    Hope this helps! :)