Hi Dave,
How to set magicparser so if 1 source feed is take long time to load (ex more than 20 sec) it will try different source?
for example this is from google news:
-----------------------------------------------------
<?php
require("MagicParser.php");
function myRecordHandler($record)
{
// This is where you write your code to process each record, such as loading a database
// You can display the record contents using PHP's internal print_r() function:
print_r($record);
// The following code will print out each field in your sample data:
print $record["TITLE"];
print $record["LINK"];
print $record["PUBDATE"];
print $record["DESCRIPTION"];
}
MagicParser_parse("{link saved}","myRecordHandler","xml|RSS/CHANNEL/ITEM/");
?>
if that take too long / more than 20 sec. it will try another source from ex:
{link saved}
Thank You..
Hi Dave,
then how to set 5 sec timeout rule for the second source, so if its more than 5 second it will pass it completely without waiting..
Thanks.
Hi,
Another ini_set line should be all that's needed; for example;
ini_set("default_socket_timeout", "20");
MagicParser_parse($primaryFeed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
if (MagicParser_getErrorMessage())
{
ini_set("default_socket_timeout", "5");
MagicParser_parse($backupFeed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
}
Cheers!
David.
i've just tested it with weird domain name for the 1st feed source so it will be passed to 2nd source.
But it doesnt show the image url. because 1st source & 2nd source have different tag name
1st source image tag named $record["MEDIA:CONTENT/MEDIA:PLAYER-URL"];
2nd source image tag named $record["CHANNEL/ITEM/MEDIA:THUMBNAIL-URL"];
how to fix that?
Thanks
Hi,
Easiest thing to do will be to set a global variable with the image tag name, then you can use that - for example:
<?php
require("MagicParser.php");
function myRecordHandler($record)
{
global $imageField;
print $record[$imageField];
}
$primaryFeed = "http://www.example.com/news.rss";
$backupFeed = "http://www.example.net/other/rss.xml";
$imageField = "MEDIA:CONTENT/MEDIA:PLAYER-URL";
ini_set("default_socket_timeout", "20");
MagicParser_parse($primaryFeed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
if (MagicParser_getErrorMessage())
{
ini_set("default_socket_timeout", "5");
$imageField = "CHANNEL/ITEM/MEDIA:THUMBNAIL-URL";
MagicParser_parse($backupFeed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
}
?>
Hope this helps!
Cheers,
David.
Wooo, you are smart, man..
btw, i got 1 weird feed. usually in 1 feed u got same tag name for each item. but this one is not same. maybe because its feed combination,
{link saved}
P.S. Please hide the link..
so the third item doesn't show the image..
and 1 more.. how if after that the image data still empty.. so it use default image. how to combine all this things?
Thnaks
Hi,
The best thing to do is simply to check them in turn using an elseif construct; for example:
function myRecordHandler($record)
{
if ($record["MOST_COMMON_IMAGE_FIELD"])
{
// code to use "MOST_COMMON_IMAGE_FIELD"
}
elseif($record["SECOND_MOST_COMMON_IMAGE_FIELD"])
{
// code to use SECOND_MOST_COMMON_IMAGE_FIELD
}
else
{
// code to display default image
}
}
Hope this helps!
Cheers,
David.
Hi dave
how to change this script so it try the backupfeed if the primaryfeed dont give any error (just blank page)
------------------------
if (MagicParser_getErrorMessage())
{
MagicParser_parse($backupFeed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
}
------------------------
that script works great when the site gives error code.. but its not working if the site gives blank page..
I'm really sorry to bother you with all this problem.. :(
Hi Demarco,
What you need to do in that case is set a global flag to show you that the parse was OK by setting it to TRUE in myRecordHandler, and then you can use the backup feed if it is not set, even if there was no error. For example;
<?php
require("MagicParser.php");
function myRecordHandler($record)
{
global $feedOK;
$feedOK = TRUE;
}
$primaryFeed = "http://www.example.com/news.rss";
$backupFeed = "http://www.example.net/other/rss.xml";
ini_set("default_socket_timeout", "20");
MagicParser_parse($primaryFeed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
if (MagicParser_getErrorMessage() || !$feedOK)
{
ini_set("default_socket_timeout", "5");
MagicParser_parse($backupFeed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
}
?>
Cheers!
David.
Hi Dave,
its not working, unless i changed:
if (MagicParser_getErrorMessage() || !$feedOK)
to:
if (MagicParser_getErrorMessage() || $feedOK)
is it correct?
Hi Demarco,
That would run the second feed when the first feed did generate output, but if that's working how you want it that's fine!
Cheers,
David.
ya it will run the second feed.. i'm sctracthing my head now..
it will more use my resources then..
can u please take a look:
<?php
require("../../MagicParser.php");
function myRecordHandler($record)
{
// This is where you write your code to process each record, such as loading a database
// You can display the record contents using PHP's internal print_r() function:
print_r($record);
// The following code will print out each field in your sample data:
print $record["ITEM"];
print $record["TITLE"];
print $record["LINK"];
print $record["DESCRIPTION"];
print $record["PUBDATE"];
}
{
global $feedOK;
$feedOK = TRUE;
}
// Please hide the link
$primaryFeed = "{link saved}";
$backupFeed = "{link saved}";
ini_set("default_socket_timeout", "20");
MagicParser_parse($primaryFeed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
if (MagicParser_getErrorMessage() || $feedOK)
{
ini_set("default_socket_timeout", "5");
MagicParser_parse($backupFeed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
}
?>
BTW, do you provide outsourcing service also?..
Thanks
Hi Demarco,
The position of the code was not quite right - have a go with:
<?php
require("../../MagicParser.php");
function myRecordHandler($record)
{
global $feedOK;
$feedOK = TRUE;
// This is where you write your code to process each record, such as loading a database
// You can display the record contents using PHP's internal print_r() function:
print_r($record);
// The following code will print out each field in your sample data:
print $record["ITEM"];
print $record["TITLE"];
print $record["LINK"];
print $record["DESCRIPTION"];
print $record["PUBDATE"];
}
// Please hide the link
$primaryFeed = "{link saved}";
$backupFeed = "{link saved}";
ini_set("default_socket_timeout", "20");
MagicParser_parse($primaryFeed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
if (MagicParser_getErrorMessage() || !$feedOK)
{
ini_set("default_socket_timeout", "5");
MagicParser_parse($backupFeed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
}
?>
...see where I moved the code to set $feedOK = TRUE...
Cheers!
David.
Hi,
What you could do is set the default socket timeout to 20 seconds, and then check for an error from the first feed and parse the backup feed if it occurred. For example;
<?php
require("MagicParser.php");
function myRecordHandler($record)
{
// This is where you write your code to process each record, such as loading a database
// You can display the record contents using PHP's internal print_r() function:
print_r($record);
// The following code will print out each field in your sample data:
print $record["TITLE"];
print $record["LINK"];
print $record["PUBDATE"];
print $record["DESCRIPTION"];
}
$primaryFeed = "http://www.example.com/news.rss";
$backupFeed = "http://www.example.net/other/rss.xml";
ini_set("default_socket_timeout", "20");
MagicParser_parse($primaryFeed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
if (MagicParser_getErrorMessage())
{
MagicParser_parse($backupFeed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
}
?>
Hope this helps!
Cheers,
David.