'md5', 'contactemail' => 'admin@example.com', 'dbtable' => 'feedurls', 'dbfilename' => 'itnfedb.sqdb' ); //----------------------------------------- // the base URL $url = ($_GET['terms'] != null) ? urldecode(urldecode($_GET['terms'])) : urldecode(urldecode($_SERVER['QUERY_STRING'])); $urlhash = hash($_SETT['hashalgo'], $url); //----------------------------------------- //Functions function curlfeed($churl) { $ch = curl_init(); curl_setopt_array ($ch, array( CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_FOLLOWLOCATION => TRUE, CURLOPT_MAXREDIRS => 10, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_URL => $churl, CURLOPT_USERAGENT => 'iTunes/9.1.1' )); $chresult = curl_exec($ch); curl_close($ch); return $chresult; } //END function: curlfeed function subURL($urltosub, $string1 = 'feed-url="', $string2 ='"') { $str1 = strpos($urltosub, $string1) + strlen($string1); $str2 = strpos($urltosub, $string2, $str1); $subbedstring = substr($urltosub, $str1, ($str2 - $str1)); return $subbedstring; } //END function: subURL function checkFEED($thefeed, $mainURL, $isCached = NULL) { global $feedURL, $invalidURL, $_SETT; //For your reference, the following is called a HEREDOC $badURL = <<iTunes failed to return the feed-url.

This may be due to different reasons:
  1) the podcast has been deleted,
  2) the podcast has no items,
  3) the podcast may be outside the USA,
and many other reasons im not aware of. Unfortunately iTunes isnt perfect (hence why your here).

Im sorry my script was of no help to you. If you feel this was in error, you can view the source,
or you can always email me the URL and i will see if its fixable. BADURL; if (substr($thefeed, 0, 4) != "http") { if (is_null($isCached)) { $feedURL = $badURL; } //END if: isCached $invalidURL = "The cake is a lie."; } //END if: http } //END function: checkFEED //----------------------------------------- //Here we initiate the sqlite database and setup the cached variables if ($database = sqlite_open($_SETT['dbfilename'], 0666, $sqlerror)) { $check_cache_query = "SELECT url FROM {$_SETT['dbtable']} WHERE uniqueid='$urlhash'"; $cache_file = @sqlite_single_query($database, $check_cache_query, true); if(sqlite_last_error($database)) { $create_table_query = "CREATE TABLE {$_SETT['dbtable']}(uniqueid TEXT, url TEXT, date TEXT)"; @sqlite_exec($database, $create_table_query); $cache_file = NULL; } //END if: sqlite last error } //END if: sqlite open else { $failed_to_initialize_sqlite = $sqlerror; } //END if: sqlite open //----------------------------------------- //For caching files, this determines if the cached file already exists if ($cache_file == NULL || isset($failed_to_initialize_sqlite)) { //----------------------------------------- //Here we identify the podcast url scheme $idstyles = array( '?id=', '&id=', '/id' ); for ($counter = 0; $counter < count($idstyles); $counter++) { if (strpos($url, $idstyles[$counter])) { $idstyle = $idstyles[$counter]; $validID = "So, how are you holding up? Because I'm a potato!"; break; } //END if: idstyles } //END for: counter //----------------------------------------- //Since iTunes-U uses the same identifier symbols, //this is where we rule them out until it is supported //Note: more checking for itunes-u content is done farther below if (strpos($url, '/itunes-u/')) { unset($validID); $invalidID = "itunes-u"; } //END if: itunes-u //----------------------------------------- // extract feed-id, get page from itunes & find feed-url if (isset($validID)) { for ($loopcount = 1; $loopcount < 3; $loopcount++) { preg_match("/[0-9]+/", $url, $podid, 0, strpos($url, $idstyle)); // here we extract the feed ID $curled1 = curlfeed("http://itunes.apple.com/podcast/id".$podid[0]); if (strpos($curled1, "kindGoto") > 1) { $newURL = subURL($curled1, "url", ""); $curled1 = curlfeed($newURL); } //END if:goto if (strpos($curled1, 'not currently available in the U.S. store')) { $feedURL = <<This item is not available in the US store.

(This may also be a problem with the podcast not existing in the iTunes store at all. Unfortunately iTunes returns the same error for both so they are indistinguishable from each other.) Apple currently restricts access to some content based on geographic locations. As I reside in the United States, I cannot retrieve your podcast link.

If you have any PHP knowledge you can try translating the public source code to retrieve the URL on your own:
http://snipplr.com/view/52465 ITUNESFOREIGNSTORE; $invalidID = 'itunes_foreign'; } //END if: us store elseif (strpos($curled1, '0 Items')) { $emptyPodcastText = <<Empty podcast.

Your podcast contains no episodes at the time this script last checked. Unfortunately Apple does not provide the feed URL for empty podcasts. Please check back in as little as 12 hours or once the podcast has active items. EMPTYPODCAST; $feedURL = $emptyPodcastText . "\n" . (time() + 60 * 60 * 12); $invalidID = 'empty_podcast'; } //END if: 0 items elseif (strpos($curled1, ' messageYour request is temporarily unable to be processed.') || strlen($curled1) < 20) { if ($loopcount == 2) { $feedURL = <<Temporarily unable to process.

iTunes returned the following error: "Your request is temporarily unable to be processed." Please try again later. CANTPROCESS; $invalidID = $doNotCacheResults = TRUE; } //END if: loopcount } //END if: unable to process elseif (strpos($curled1, 'iTunes U')) { //Leave this as the last elseif as it may catch non-iTunesU material $itunesU_title = subURL($curled1, '', ''); $itunesU_title_char = urlencode($itunesU_title); $itunesU_crumbs = subURL($curled1, 'start breadcrumbs', 'end breadcrumbs'); $itunesU_li = subURL($itunesU_crumbs, '
  • ', '
  • '); if (strpos($itunesU_li, 'iTunes U')) { $feedURL = <<iTunes-U links not supported.

    Currently Apple does not offer a way to subscribe to iTunes-U material outside of iTunes (that i can find). A temporary solution is to search for a similar title as a podcast in hopes that the content providers also posted it to the iTunes Podcast Directory (do no expect this for password protected content). Try searching for:
    "$itunesU_title"

    You can click here to try that now. ITUNESU; $invalidID = 'itunes-u'; } //END if: itunesu_li } //END if: iTunes U if (!isset($invalidID)) { $feedURL = subURL($curled1); checkFEED($feedURL, $url); if (isset($invalidURL) && $loopcount == 1) { unset($invalidURL, $podid); sleep(2); } // END if: loopcount else { break; } //END if: loopcount } //END if: invalidID else { break; } //END if: invalidID } //END for: loopcount if (!isset($failed_to_initialize_sqlite) && !isset($doNotCacheResults)) { $newfeedURL = sqlite_escape_string($feedURL); $cache_query_put_content = "INSERT INTO {$_SETT['dbtable']} (uniqueid,url,date) VALUES ('$urlhash', '$newfeedURL', '".date("r")."'"; @sqlite_exec($database, $cache_query_put_content); } //END if: failed sqlite } //END if: validID else { if ($invalidID == "itunes-u") { //Example URL: http://itunes.apple.com/itunes-u/the-civil-war-reconstruction/id341650730 $itu_label = subURL($url, '/itunes-u/', '/'); $itu_label_white = trim(ucwords(str_replace('-', ' ', $itu_label))); $itu_label_char = str_replace('-', '%20', $itu_label); $feedURL = <<iTunes-U links not supported.

    Currently Apple does not offer a way to subscribe to iTunes-U material outside of iTunes (that i can find). A temporary solution is to search for a similar title as a podcast in hopes that the content providers also posted it to the iTunes Podcast Directory (do no expect this for password protected content). Try searching for:
    "$itu_label_white"

    You can click here to try that now. FEEDURL; } //END if: itunes-u else { $feedURL = "URL not supported.

    Please contact {$_SETT['contactemail']}
    and notify me of the URL you are trying."; $invalidID = "I was doing fine. Noone was trying to murder me, or put me in a potato, or feed me to birds."; } //END if: itunes-u } //END if: validID } //END if: cache_file else { $feedURL = $cache_file; if (strpos($feedURL, 'Empty podcast.')) { $invalidID = TRUE; $feedURL_explode = explode("\n", $feedURL); if (time() > (int)trim(end($feedURL_explode))) { $delete_table_query = "DELETE FROM {$_SETT['dbtable']} WHERE uniqueid='$urlhash'"; @sqlite_exec($database, $delete_table_query); header("Location: " . $_SERVER["SCRIPT_URI"] . "?" . $_SERVER["QUERY_STRING"]); exit; } //END if: time else { $feedURL = implode("\n", explode("\n", $feedURL, -1)); } //END if: time } //END if: empty podcast elseif (stripos($feedURL, 'itunes-u')) { $invalidID = 'itunes-u'; } //END elseif: itunes-u else { checkFEED($feedURL, $url, TRUE); } //END if: empty podcast } //END if: cache_file //----------------------------------------- // html output to browser $podcastURL = (isset($invalidURL) || isset($invalidID)) ? ($invalidID == 'empty_podcast' ? $emptyPodcastText : $feedURL) : "$feedURL"; echo << Podcast URL

    Podcast URL:

    OUTTEXT; ?>