Parse XML/PHP

The final functioning product:
[php]

<?php
/*

HNE RSS Aggragator - October 7, 2013

Haslage Net Enterprises
http://www.haslage.net/

Credits: Anthony Haslage & JimL (PHPHelp.Com)

Features (Requires PHP5 & CURL):

  • Saves the file locally to get around blocked remote file inclusions.
  • Converts time from milliseconds to seconds.
  • Offers variables to offset timezones.
    ======================================================================
    LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

To read the license please visit http://www.gnu.org/copyleft/gpl.html

*/

// File to be downloaded.
$ch = curl_init(“http://www.website.com/data.xml”);
// Location to save data (Create the file & CHMOD 777).
$fp = fopen(“data-output.html”, “w”);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

// Location to load data (Should be the same as where the data was previously saved).
$xml = file_get_contents(‘data-output.html’);
$results = new SimpleXMLElement($xml);
// HTML before all items.
print “”;
foreach ($results->items->item as $item)
{
$time = (float) $item->time / 1000; // Convert from milliseconds to seconds.
$end = ((float) $item->time + (float) $item->duration) / 1000; // Add begin time and duration to produce end time, then convert from milliseconds to seconds.
$tz = (float) $item->utc_offset / 1000; // Convert from milliseconds to seconds.
$timetz = $time + $tz; // Add start time and timezone to correct offset.
$endtz = $end + $tz; // Add end time and timezone to correct offset.

$dt = new DateTime("@$timetz"); // Convert UNIX timestamp to PHP DateTime
$newtime = $dt->format('M. j, Y @ g:i A'); // Display date and time begin
$ee = new DateTime("@$endtz"); // Convert UNIX timestamp to PHP DateTime
$newend = $ee->format('g:i A'); // Display time end

$venue = $item->venue->name; // List venue since code does not like printing variables more than one level into XML tree.

// For output testing purposes.
// print “Time Begin: $time
Time End: $end
Timezone: $tz
”;
// print “Time (ADJ): $timetz
Date Begin: $newtime
Date End: $newend
”;
// print “Name: $item->name
Description: $item->description
URL: $item->event_url
Location: $item->venue->name
”;

print "<li><a href=\"$item->event_url\" target=\"newwindow\" title=\"$item->description\">$item->name - $newtime to $newend ($venue)</a></li>\n";

// print “

”; var_dump($item); // For variable testing purposes.
}
// HTML after all items.
print “”;
?>
[/php]

Great, glad you learned something.

btw: If you don’t have to store a local copy then you can just parse the result from curl_download directly :slight_smile:

Also this:
[php] // Location to save data (Create the file & CHMOD 777).
$fp = fopen(“data-output.html”, “w”);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

// Location to load data (Should be the same as where the data was previously saved).
$xml = file_get_contents(‘data-output.html’);[/php]

Could be changed to this:
[php] // Location to save data (Create the file & CHMOD so it’s read/writable by the web service).
$xmlStore = ‘data-output.html’;

$fp = fopen($xmlStore, “w”);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

// Location to load data (Should be the same as where the data was previously saved).
$xml = file_get_contents($xmlStore);[/php]

That way you remove a potential problem when changing data location (if you forget to update one of the two)

Also you should never set files to chmod 777. It should be set properly according to your hosting environment.

Thanks for the xmlStore tip, I will apply it.

For some reason, the server did not like any other permissions. If anyone is worried, they can save the files outside of the web directory, like I do.

Thanks again.

Regarding the permissions. Are you sure you gave permissions to the user the web server is running as? Perhaps you’ve created the files as user “anthony” who is in group “users”, but the web server runs as user “apache” in the group “www-data”. In this case you will have to have at least read+write to all.

Instead you should chown the files to anthony:www-data (giving the web server user access via group) and do a 760 permission.

as it is now every user / site on the server can read/write to your file(s)

Updated:

[php]

<?php
/*

HNE RSS Aggragator - October 10, 2013

Haslage Net Enterprises
http://www.haslage.net/

Credits: Anthony Haslage & JimL (PHPHelp.Com)

Features (Requires PHP5 & CURL):

  • Saves the file locally to get around blocked remote file inclusions.
  • Converts time from milliseconds to seconds.
  • Offers variables to offset timezones.
    ======================================================================
    LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

To read the license please visit http://www.gnu.org/copyleft/gpl.html

*/

// File to be downloaded.
$ch = curl_init(“data.xml”);
// Location to save data (Create the file & CHMOD 777 - Save the file outside of your web directory).
$xmlStore = ‘data-saved.html’;
$fp = fopen($xmlStore, “w”);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

$xml = file_get_contents($xmlStore);
$results = new SimpleXMLElement($xml);
// HTML before all items.
print “”;
foreach ($results->items->item as $item)
{
$time = (float) $item->time / 1000; // Convert from milliseconds to seconds.
$end = ((float) $item->time + (float) $item->duration) / 1000; // Add begin time and duration to produce end time, then convert from milliseconds to seconds.
$tz = (float) $item->utc_offset / 1000; // Convert from milliseconds to seconds.
$timetz = $time + $tz; // Add start time and timezone to correct offset.
$endtz = $end + $tz; // Add end time and timezone to correct offset.

$dt = new DateTime("@$timetz"); // Convert UNIX timestamp to PHP DateTime
$newtime = $dt->format('M. j, Y @ g:i A'); // Display date and time begin
$ee = new DateTime("@$endtz"); // Convert UNIX timestamp to PHP DateTime
$newend = $ee->format('g:i A'); // Display time end

$venue = $item->venue->name; // List venue since code does not like printing variables more than one level into XML tree.

// For output testing purposes.
// print “Time Begin: $time
Time End: $end
Timezone: $tz
”;
// print “Time (ADJ): $timetz
Date Begin: $newtime
Date End: $newend
”;
// print “Name: $item->name
Description: $item->description
URL: $item->event_url
Location: $item->venue->name
”;

// Headline formatted output.
print “

  • <a href=”$item->event_url" target=“newwindow” title="$item->description">$item->name - $newtime to $newend ($venue)
  • \n";

    // print “

    ”; var_dump($item); // For variable testing purposes.
    }
    // HTML after all items.
    print “”;
    ?>
    [/php]

    Great, happy with the result? :slight_smile:

    Btw: you don’t have to credit me ^^

    On the permissions topic… I believe that you are right about how the user and group are incorrectly set, but I have no control of them, so I cannot change them. The best I was able to do and everything still work was 766.

    Also, I always try to give credit where it is due.

    You could consider changing web host host :slight_smile:

    It is possible to setbup a shared hosting environment where apache runs as the current user. That means you could actually do chmod 700 :slight_smile:

    Thanks for the advice, but I have been with this host for over a decade. It is only people I know on my server. Keeping the writable files out of my web directory is an easier and least costly solution.

    Okay, I need to ask for one more thing. This code is working great, but I need a choke on it. I need to be able to set how many entries it will display to the three newest. As always, any help is appreciated, thanks.

    Are the entries sorted new-> old? If so just add an incrementing number to the loop and break out fter 3 iteriations.

    If not you have to sort it first

    They are default listed oldest (bottom) to newest (top). So I guess I just need something to simply limit the display to the top 3 newest items. Thanks.

    Sponsor our Newsletter | Privacy Policy | Terms of Service