Rss feed help

Discussion in 'PHP' started by fadetoblack22, Jun 15, 2008.

  1. #1
    I have the follow code that I was given to display an rss feed. When I test it, it says

    "Parse error: syntax error, unexpected '[' in /home/wayneroo/public_html/bigfreebet.com/test44.php on line 6"

    If I take out the [] on line 6 which is: $max = [6]; /* Number results outputted */, I says no feed available, which is not right. I have tried it with lots of feeds and none work.

    Could someone have a look for me please :)

    <?PHP 
    
    $feed	= "[http://rss.paddypower.com/rss/AffiliateFootballSteamers.xml]";
    $AFF_ID = "[10001227]";
    $noresults     = "[NO RESULTS MESSAGE]";
    $max = [6];    /* Number results outputted */ 
    
    $count         = 0; 
    
    // Create an XML parser 
    $xml_parser = xml_parser_create(); 
    
    // Set the functions to handle opening and closing tags 
    xml_set_element_handler($xml_parser, "startElement", "endElement"); 
    
    // Set the function to handle blocks of character data 
    xml_set_character_data_handler($xml_parser, "characterData"); 
    
    // Open the XML file for reading 
    $fp = @fopen($feed ,"r") or die("No feed available."); 
    
    // Read the XML file 4KB at a time 
    while ($data = fread($fp, 4096)) { 
    
       // Parse each 4KB chunk with the XML parser created above 
       xml_parse($xml_parser, $data, feof($fp)) 
           // Handle errors in parsing 
           or die(sprintf("XML error: %s at line %d", 
               xml_error_string(xml_get_error_code($xml_parser)), 
               xml_get_current_line_number($xml_parser))); 
    } 
    
    // Close the XML file 
    fclose($fp); 
    
    // Free up memory used by the XML parser 
    xml_parser_free($xml_parser); 
    
    if($count == 0) { 
        echo $noresults; 
    } 
    
    function startElement($parser, $name, $attrs) { 
        global $insideitem, $tag, $title, $description, $link; 
        if ($insideitem) { 
            $tag = $name; 
        } elseif ($name == "ITEM") { 
            $insideitem = true; 
        } 
    } 
    function endElement($parser, $name ) { 
        global $insideitem, $tag, $title, $description, $link, $AFF_ID, $max, $count; 
        if ($name == "ITEM" && $count < $max) { 
            printf("<b><a href='%s'>%s</a></b><br>", trim($link)."&AFF_ID=".$AFF_ID,htmlspecialchars(trim($title))); 
            printf("%s<br><br>", html_entity_decode(trim($description))); 
            $title = ""; 
            $description = ""; 
            $link = ""; 
            $insideitem = false; 
            $count++; 
        } 
    } 
    
    function characterData($parser, $data) { 
        global $insideitem, $tag, $title, $description, $link; 
        if ($insideitem) { 
            switch ($tag) { 
                case "TITLE": 
                $title .= $data; 
                break; 
                case "DESCRIPTION": 
                $description .= $data; 
                break; 
                case "LINK": 
                $link .= $data; 
                break; 
            } 
        } 
    } 
    
    ?>  
    PHP:

     
    fadetoblack22, Jun 15, 2008 IP
  2. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Try removing the brackets around 6
    Change
    
    $max = [6]; 
    
    PHP:
    to
    
    $max=6;
    
    PHP:
     
    rohan_shenoy, Jun 15, 2008 IP
  3. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #3
    As I said, I have tried that and it just says "no feeds available".

    I tested it with lots of working feeds to see if this was true and it says it on all of them, so its going wrong somewhere in the code.
     
    fadetoblack22, Jun 15, 2008 IP
  4. coffeesonnow

    coffeesonnow Peon

    Messages:
    34
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <?PHP

    $feed = "http://rss.paddypower.com/rss/AffiliateFootballSteamers.xml";
    $AFF_ID = "[10001227]";
    $noresults = "[NO RESULTS MESSAGE]";
    $max = 6; /* Number results outputted */

    $count = 0;

    // Create an XML parser
    $xml_parser = xml_parser_create();

    // Set the functions to handle opening and closing tags
    xml_set_element_handler($xml_parser, "startElement", "endElement");

    // Set the function to handle blocks of character data
    xml_set_character_data_handler($xml_parser, "characterData");

    // Open the XML file for reading
    $fp = fopen($feed ,"r") or die("No feed available.");

    // Read the XML file 4KB at a time
    while ($data = fread($fp, 4096)) {

    // Parse each 4KB chunk with the XML parser created above
    xml_parse($xml_parser, $data, feof($fp))
    // Handle errors in parsing
    or die(sprintf("XML error: %s at line %d",
    xml_error_string(xml_get_error_code($xml_parser)),
    xml_get_current_line_number($xml_parser)));
    }

    // Close the XML file
    fclose($fp);

    // Free up memory used by the XML parser
    xml_parser_free($xml_parser);

    if($count == 0) {
    echo $noresults;
    }

    function startElement($parser, $name, $attrs) {
    global $insideitem, $tag, $title, $description, $link;
    if ($insideitem) {
    $tag = $name;
    } elseif ($name == "ITEM") {
    $insideitem = true;
    }
    }
    function endElement($parser, $name ) {
    global $insideitem, $tag, $title, $description, $link, $AFF_ID, $max, $count;
    if ($name == "ITEM" && $count < $max) {
    printf("<b><a href='%s'>%s</a></b><br>", trim($link)."&AFF_ID=".$AFF_ID,htmlspecialchars(trim($title)));
    printf("%s<br><br>", html_entity_decode(trim($description)));
    $title = "";
    $description = "";
    $link = "";
    $insideitem = false;
    $count++;
    }
    }

    function characterData($parser, $data) {
    global $insideitem, $tag, $title, $description, $link;
    if ($insideitem) {
    switch ($tag) {
    case "TITLE":
    $title .= $data;
    break;
    case "DESCRIPTION":
    $description .= $data;
    break;
    case "LINK":
    $link .= $data;
    break;
    }
    }
    }

    ?>



    Seems to work :)
     
    coffeesonnow, Jun 15, 2008 IP
    fadetoblack22 likes this.
  5. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #5
    thanks, thats great!

    do you know how to make the feed links target="_blank" and nofollow?

    thanks
     
    fadetoblack22, Jun 16, 2008 IP
  6. coffeesonnow

    coffeesonnow Peon

    Messages:
    34
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well, I haven't tried this out, but on this line:

    printf("<b><a href='%s'>%s</a></b><br>", trim($link)."&AFF_ID=".$AFF_ID,htmlspecialchars(trim($title)));

    I would simply change it to

    printf("<b><a href='%s' target=\"_blank\" rel=\"nofollow\">%s</a></b><br>", trim($link)."&AFF_ID=".$AFF_ID,htmlspecialchars(trim($title)));
     
    coffeesonnow, Jun 16, 2008 IP
  7. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #7
    Thanks, it works! :)

    Do you know how to do the same in a dropdown list?

    It doesn't seem to work the same :(


    http://www.play-hookey.com/htmltest/


    <form name="htmlMenu">
    
     <select name="htmlSelList" size="1">
    
    <option value="">Please select...</option>
                                                                      
    <option value="http://www.google.com">Google</option>
    
    </select>
                                                                      
    <input type="button" onClick="document.location =document.htmlMenu.htmlSelList.options [document.htmlMenu.htmlSelList.selectedIndex].value;"value="GO">
                                                                      
        </form>
    PHP:
     
    fadetoblack22, Jun 17, 2008 IP
  8. coffeesonnow

    coffeesonnow Peon

    Messages:
    34
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hmm... I'm not very familiar with drop downs... It's more of javascript than php.
     
    coffeesonnow, Jun 18, 2008 IP
  9. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #9
    Ok, thanks anyway. I will have a look around :)
     
    fadetoblack22, Jun 18, 2008 IP