Hi, im new here , and im new to php
im trying to parse a page and write the output to a txt file
<?php
require("MagicParser.php");
$txt = "";
function myRecordHandler($record)
{
if (isset($record["TITLE"])) {
$txt = $txt . $record["TITLE"]."|;|";
} else {
$txt = $txt . "False"."|;|" ;
}
if (isset($record["DESCRIPTION"])) {
$txt = $txt . $record["DESCRIPTION"]."|;|";
} else {
$txt = $txt . "False"."|;|" ;
}
if (isset($record["LINK"])) {
$txt = $txt . $record["LINK"]."|;|";
} else {
$txt = $txt . "False"."|;|" ;
}
if (isset($record["MEDIA:THUMBNAIL-URL"])) {
$txt = $txt . $record["MEDIA:THUMBNAIL-URL"]."|;|";
} else {
$txt = $txt . "False"."|;|" ;
}
if (isset($record["PUBDATE"])) {
$txt = $txt . $record["PUBDATE"]."|:|" . "<br>";
} else {
$txt = $txt . "False"."|:|". "<br>" ;
}
echo "hi";
}
$url = "http://feeds.bbci.co.uk/arabic/rss.xml";
MagicParser_parse($url,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
fwrite($myfile, $txt);
fclose($myfile);
echo $txt;
?>
what im doing wrong ?
Hello fraizor and welcome to the forum!
I think the only problem is that $txt is not declared as global at the top of the myRecordhandler() function, so the $txt that is being constructed is a new variable for every record and not visible outside the function. Have a go with:
<?php
require("MagicParser.php");
$txt = "";
function myRecordHandler($record)
{
global $txt;
if (isset($record["TITLE"])) {
$txt = $txt . $record["TITLE"]."|;|";
} else {
$txt = $txt . "False"."|;|" ;
}
if (isset($record["DESCRIPTION"])) {
$txt = $txt . $record["DESCRIPTION"]."|;|";
} else {
$txt = $txt . "False"."|;|" ;
}
if (isset($record["LINK"])) {
$txt = $txt . $record["LINK"]."|;|";
} else {
$txt = $txt . "False"."|;|" ;
}
if (isset($record["MEDIA:THUMBNAIL-URL"])) {
$txt = $txt . $record["MEDIA:THUMBNAIL-URL"]."|;|";
} else {
$txt = $txt . "False"."|;|" ;
}
if (isset($record["PUBDATE"])) {
$txt = $txt . $record["PUBDATE"]."|:|" . "<br>";
} else {
$txt = $txt . "False"."|:|". "<br>" ;
}
echo "hi";
}
$url = "http://feeds.bbci.co.uk/arabic/rss.xml";
MagicParser_parse($url,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
fwrite($myfile, $txt);
fclose($myfile);
echo $txt;
?>
Hope this helps!
Cheers,
David
--
MagicParser.com