Hi all,
I have a script that takes a podcast xml file and parses out each item's data for inclusion into a MySQL database. I have been having problems populating a field with the subfolder name, and thanks to help here from others successfully wrote a script to parse that subfolder name out of the full path. However, when I added that custom function to my full script, it chokes after the first podcast.
If I comment out the custom function and go back to the entry that leaves the folder info blank, if loops through all four podcasts in my sample file.
What is it that I'm missing? I've tried it both through an inclusion file, as well as pasting the custom function directly into my main script.
Thanks for any help anyone can give me on this!
Robert
Here's the code of my full script:
<?php
// opens $myFile and assigns its content to $theData
$myFile = "Habakkuk.xml"; //PODCAST XML FILE WITH 4 PODCASTS
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$strCode = $theData;
if (preg_match_all('%<item>.*?</item>%si',$strCode,$regs)) {
$aryItems = $regs[0];
foreach($aryItems as $strItem) {
$strTitle = '';
$strDescription = '';
$entry = '';
$strLink = '';
$strGUID = '';
$strEnclosureURL = '';
$intEnclosureLength = '';
$strEnclosureType = '';
$strPubDate = '';
$strpubZoneOffset = '';
$strblockEpisode = 'no';
$strisClosedCaptioned ='no';
$aryItuneData = array();
if (preg_match('%<title>(.*?)</title>%si',$strItem,$regs)) {
$strTitle = $regs[1];
}
if (preg_match('%<description>(.*?)</description>%si',$strItem,$regs)) {
$strDescription = $regs[1];
}
if (preg_match('%<link>(.*?)</link>%si',$strItem,$regs)) {
/* IF THE FUNCTION IS USED THIS STOPS AFTER ONLY ONE PODCAST, NOT FOUR
function getDirName() {
$entry = 'http://www.recastweb.com/media/Luke/Luke14_01-14.mp3';
$path = pathinfo($entry);
$url=$path['dirname'];
$folder = parse_url($url, PHP_URL_PATH);
$entry1 = $folder;
$path1 = pathinfo($entry1);
return $path1['basename'];
}
$xyz = getDirName();
// include("return_value.php"); CONTENT OF return_value.php pasted above
*/
// IF THIS SECTION IF USED INSTEAD OF ABOVE IT PROCESSES ALL FOUR ITEMS
$entry = $regs[1];
$path = pathinfo($entry);
$string=$path['dirname'];
$folder = pathinfo($string);
}
if (preg_match('%<link>(.*?)</link>%si',$strItem,$regs)) {
$strLink = $regs[1];
}
if (preg_match('%<guid isPermaLink=\"false\">(.*?)</guid>%si',$strItem,$regs)) {
$strGUID = $regs[1];
}
if (preg_match('%<pubDate>(.*?)</pubDate>%si',$strItem,$regs)) {
$strPubDate = date('D, d M Y H:i:s',strtotime($regs[1]));
}
if (preg_match('%<pubDate>(.*?)</pubDate>%si',$strItem,$regs)) {
$strpubZoneOffset = date('O',strtotime($regs[1]));
}
if (preg_match('%<enclosure url="([^"]*?)" length="([^"]*?)" type="([^"]*?)".*?</enclosure>.%si',$strItem,$regs)) //use for </enclosure>
{
// if (preg_match('%<enclosure url="([^"]*?)" length="([^"]*?)" type="([^"]*?)".*?/>.%si',$strItem,$regs)) //use for />
// {
$strEnclosureURL = $regs[1];
$intEnclosureLength = (int)$regs[2];
$strEnclosureType = $regs[3];
}
if (preg_match_all('%<itunes:([^>]+)>(.*?)</itunes:\1>%si',$strItem,$regs)) {
foreach($regs[2] as $index=>$val) {
$aryItuneData[$regs[1][$index]] = $val;
}}
echo "\n=============== ITEM DATA ==============\n <br />";
echo "Title: $strTitle\n <br />";
echo "Description: $strDescription\n <br />";
echo "Link: $strLink\n <br />";
echo "GUID: $strGUID\n <br />";
echo "Folder: $xyz\n <br />";
echo "Enclosure URL: $strEnclosureURL\n <br />";
echo "Enclosure Size: $intEnclosureLength\n <br />";
echo "Enclsoure Type: $strEnclosureType\n <br />";
echo "PubDate: $strPubDate\n <br />";
echo "pubZoneOffset: $strpubZoneOffset\n <br />";
echo "<br />";
echo "\n=============== ITUNES DATA ==============\n <br />";
foreach($aryItuneData as $key=>$val)
echo "\t$key: $val\n <br />";{
echo "isClosedCaptioned: $strisClosedCaptioned\n <br />";
echo "blockEpisode: $strblockEpisode\n <br />";}
echo "------------------------------------------------------\n <br />";
$emptyExplicit = 'no';
$con = mysql_connect("localhost","USER","PASSWORD");
mysql_select_db("podadmin", $con);
// insertion into episodes table
$query = "INSERT INTO import (guid, title, subtitle, keywords, explicit, isClosedCaptioned, author, description, pubdate, pubZoneOffset, blockEpisode, size, duration, folder, filename)";
$query .= "VALUES (";
$query .= "'" . mysql_real_escape_string(basename($strGUID)) . "', " .
"'" . mysql_real_escape_string($strTitle) . "', " .
"'" . mysql_real_escape_string($aryItuneData['subtitle']) . "', " .
"'" . mysql_real_escape_string($aryItuneData['keywords']) . "', " .
"'" . mysql_real_escape_string($emptyExplicit) . "', " .
"'" . mysql_real_escape_string($strisClosedCaptioned) . "', " .
"'" . mysql_real_escape_string($aryItuneData['author']) . "', " .
"'" . mysql_real_escape_string($strDescription) . "', " .
"FROM_UNIXTIME('" . strtotime(mysql_real_escape_string($strPubDate)) . "'), " .
"'" . mysql_real_escape_string($strpubZoneOffset) . "', " .
"'" . mysql_real_escape_string($strblockEpisode) . "', " .
"'" . mysql_real_escape_string($intEnclosureLength) . "', " .
"'" . mysql_real_escape_string($aryItuneData['duration']) . "', " .
"'" . mysql_real_escape_string($xyz) . "', " .
"'" . mysql_real_escape_string(basename($strLink)) . "')";
if ( ! mysql_query($query) ) {
exit("Query failed! - $query<Br>" . mysql_error());
}
// CHECKING FOR DUPLICATE GUID ENTRIES IN XML FILE
$emptyExplicit = 'no';
$con = mysql_connect("localhost","USER","PASSWORD");
mysql_select_db("podadmin", $con);
// insertion into guid table
$query = "INSERT INTO guid (guid, filename) ";
$query .= "VALUES (";
$query .= "'" . mysql_real_escape_string(basename($strGUID)) . "', " .
"'" . mysql_real_escape_string(basename($strLink)) . "')";
if ( ! mysql_query($query) ) {
exit("Query failed! - $query<Br>" . mysql_error());
}
}
}
print "<p style='color: green'>Episodes were added.</p>\n";
mysql_close($con);
?>