Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

News Feed no brainer!

  • 20-03-2007 03:26PM
    #1
    Closed Accounts Posts: 8,866 ✭✭✭


    Hi guys,

    I've tried and tried again to implement this code, which I know is just example code, but should work right? The example feed is live. And I've gotten error after error after error, all relating to xml parsing I believe. At the moment its:

    Warning: xpath_eval() URL="http://localhost/goa/function.xpath-eval"]function.xpath-eval[/URL: Invalid expression in C:\Program Files\xampp\htdocs\goa\parser.php on line 30

    Why might that be? I've read up a bit, and found that this kind of functionality is dodgy stuff, solutions seem to be either non-existant, or involving extensions which I'm pretty sure I've installed correctly, and mentions of recompiling php.ini. Now I'm not exactly a n00b, but right now I'm shaggin lost! So please, somebody, put me out of my misery! :(

    Adam


Comments

  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Might be an idea to post the code for parser.php. An example of the XML that's being used when you get the error would also be valuable.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    parser.php simply contains the example code I linked to in my post, and the xml being used is within that sample code also.


  • Registered Users, Registered Users 2 Posts: 6,655 ✭✭✭daymobrew


    Does your PHP installation have php_domxml extension? (mentioned at the top of the page linked to).

    It would be worthwhile posting your code, especially to see line 30. Maybe there is a typo somewhere.

    You could also try simplify the thing e.g. start with a static XML file.
    Are you using PHP5? A user comment for xpath-new-context mentions issues wrt PHP5.


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Mirror wrote:
    parser.php simply contains the example code I linked to in my post, and the xml being used is within that sample code also.

    :rolleyes: Good luck getting it sorted then.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Here's the code in parser.php:
    [php]<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"><body><div id="pagecell1">
    <?php

    // creating connection for MySql by passing Server,Username,Password
    $connection = mysql_connect("localhost","root","");
    mysql_select_db("test", $connection);
    //Loading xml from url stream
    $dataFile = fopen ('http://feeds.directnews.co.uk/?c21c0905-b9ca-43ee-87be-7e4308406da6','r')
    or die("Error Loading XMl");
    if ( $dataFile )
    {
    while (!feof($dataFile))
    {
    $buffer .= fgets($dataFile, 4096);
    }
    fclose($dataFile);
    }
    else
    {
    die( "fopen failed for $filename" ) ;
    }
    // Loading xml into a DOM object.
    $dom = domxml_open_mem($buffer);
    $xpc = xpath_new_context($dom);

    // Selecting nodes matching node "Article" using xpath_eval command and passing it the xpath context
    $nodes = xpath_eval($xpc, "//Article/");
    // Just for debugging purposes -> print "Article found: ".count($nodes->nodeset)."<br/>";
    // For each nodes in the collection we loop through each node and retrive and save the data.
    foreach ($nodes->nodeset as $node){
    $id = $node->get_attribute("ID");
    // Just for debugging purposes -> print "ID: ".$id."<br/>";
    foreach ($node->child_nodes() as $child) {
    if ($child->node_name() == "Heading") $heading = $child->get_content();
    if ($child->node_name() == "Contents") $content = $child->get_content();
    if ($child->node_name() == "Date") $date = $child->get_content();
    }
    //replacing ' with '' for adding data to sql and stripping data and reformatting it for sql
    $content = str_replace("'", "''", $content);
    $heading = str_replace("'", "''", $heading);
    list($day, $month, $year) = split('[/]',$date);
    // Just for debugging purposes -> echo "Day : $day, Month: $month, Year: $year";
    // adding and updating the data in database, first checking if the id exist in database if it does then update the data or add an new item.
    $resultA = mysql_query("SELECT * FROM feeddata WHERE ID = '$id'");
    if(!$resultA){
    echo 'Could not run query: ' . mysql_error();
    exit;
    }
    $row = mysql_num_rows($resultA);
    if ($row == 0){
    $insert = "INSERT INTO feeddata ( ID, Content, Heading, Date) VALUES ('$id', '$content','$heading','$year-$month-$day')";
    // Just for debugging purposes -> echo $insert;
    $resultB = mysql_query ($insert , $connection);
    if (mysql_errno() == 1062) {
    echo 'could not run : '.mysql_errno();
    echo 'Could not run query: ' . mysql_error();
    exit;
    }
    }
    else{
    $result = mysql_query ("UPDATE feeddata SET Content='$content', Heading='$heading', Date='$date' where ID='$id'",$connection);
    if (!$result) {
    echo 'could not run : '.mysql_errno();
    echo 'Could not run query: ' . mysql_error();
    exit;
    }
    }
    }
    // Closing the connection after the all the data from the feed has been added.
    mysql_close($connection);
    ?>

    </div>
    </body>[/php]

    And a screenshot of my phpinfo regarding dom. I'm on an xampp installation php version 5.2.1, when I use php switch to version 4.4.5 I get this error:

    Fatal error: Call to undefined function: domxml_open_mem() in C:\Program Files\xampp\htdocs\goa\parser.php on line 26

    and I couldn't for the life of me suss out the dom_xml for php4...

    Also, I would start at the start and work down, but I have zero experience with php+xml!

    Thanks for the responses so far :)


  • Advertisement
  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Em, bump?

    Can anyone even look at the the feed and show me how to access it on the most baic level? I'm sure I can work up from there.


Advertisement