You are here:  » Odd Request


Odd Request

Submitted by shawnwalters on Tue, 2007-03-27 04:46 in

Ok, so I'm not sure if this is even possible, but I'm looking to check our own internal PPC feed for a conditional. And if that conditional is met, display the results that match the conditional. If not include a completely different feed url. In this instance, the conditional is the bid price of a PPC listing. So for instance something like:

if ($result['bid'] > .50){
do normal results } else {
include other feed }

is something like that possible? Here is sample code from the feed:

<?
<RESULTS>
<LISTING>
<TITLE>Test Title</TITLE>
<URL>test.com</URL>
<REDIRECT>www.test.com/redirect</REDIRECT>
<DESCRIPTION>Blah Blah</DESCRIPTION>
<BID>0.81</BID>
</LISTING>
?>

Thanks,
Shawn

Submitted by support on Tue, 2007-03-27 13:44

Hi Shawn,

That sounds straight forward enough, although i'm not quite sure what actions you wanted to take based on the outcome of the test. However, to perform the test, something like this should work:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
$bid floatval($record["BID"]);
    if (
$bid 0.5)
    {
      
// do normal results here
    
}
    else
    {
      
// here if bid less than 0.5 for this record
    
}
  }
  
MagicParser_parse("results.xml","myRecordHandler","xml|RESULTS/LISTING/");
?>

Now the conditional test in myRecordHandler is going to happen for every LISTING in results.xml. If there is only ever one listing, then you could assign the result of the test to a global variable, and then use that variable after the call to MagicParser_parse() in order to decide what to do next...

Hope this helps!
Cheers,
David.

Submitted by shawnwalters on Tue, 2007-03-27 17:35

Hi David,

Thanks that does help. The only thing I'm not sure about is that the "else" would be including a completely different feed. And if it did that for each result and there were 10 results it would try to include that feed 10 times. Is there a way to do this so that it would only include or execute that "else" code once?

Thanks,
Shawn

Submitted by shawnwalters on Tue, 2007-03-27 23:05

Ok, one more question, I can't seem to parse this url correctly:

http://www.uncoverthenet.com/ad/search.php?fmt=xml&IP=67.11.11.111&hpp=2&Terms=cars

I'm assuming it's because of the field? Any way to parse this file?

Thanks again,
Shawn

Submitted by support on Wed, 2007-03-28 08:41

Hi Shawn,

Regarding parsing another feed, this is where you would want to bring the result of the test into a global variable and then use if after the first parse to decide whether to parse the second feed:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    global 
$testFailed;
    
$bid floatval($record["BID"]);
    if (
$bid 0.5)
    {
      
// do normal results here
    
}
    else
    {
      
// here if bid less than 0.5 for this record
      
$testFailed TRUE;
    }
  }
  
MagicParser_parse("results.xml","myRecordHandler","xml|RESULTS/LISTING/");
  function 
mySecondRecordHandler($record)
  {
    
// process records in the second feed here if you can't use
    // the same record handler as the previous parse
  
}
  if (
$testFailed)
  {
    
// now parse your second file
    
MagicParser_parse("nextfile.xml","mySecondRecordHandler","xml|FORMAT/STRING/");
  }
?>

If you can use the same record handler function then just use "myRecordHandler" in the second call to MagicParser_parse()...

Cheers,
David.

Submitted by support on Wed, 2007-03-28 08:52

Regarding this feed:

http://www.uncoverthenet.com/ad/search.php?fmt=xml&IP=67.11.11.111&hpp=2&Terms=cars

The XML is badly formatted, and ultimately you would need to contact the company providing the feed and ask them to format it correctly. The problem is that the URL in the REDIRECT field is contains characters that are significant within an XML document, and this breaks the formatting.

The only thing to do in this situation is the load the XML into a string, manually fix the problem using PHP string functions and then parse the string. Here's what I mean:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($item)
  {
    
print_r($item);
  }
  
$url "http://www.uncoverthenet.com/ad/search.php?fmt=xml&IP=67.11.11.111&hpp=2&Terms=cars";
  
// retrieve the XML into a string
  
$source fopen($url,"r");
  while(!
feof($source)) $xml .= fread($source,256);
  
fclose($source);
  
// fix up the XML, in this case change the & character into the correct entity
  
$xml str_replace("&","&amp;",$xml);
  
// parse using Magic Parser's built in string:// protocol handler
  
MagicParser_parse("string://".$xml,"myRecordHandler","xml|RESULTS/LISTING/");
?>

The above code processes the results correctly as if you had parsed it directly from the URL and the XML wasn't broken! The fix that needs to be applied is to convert the & character into the appropriate XML entity.

Hope this helps!
Cheers,
David.