You are here:  » Blank page, no errors - script worked fine previously with different XML


Blank page, no errors - script worked fine previously with different XML

Submitted by Benjip on Sun, 2009-02-22 04:59 in

I'm having a nightmare trying to get a new style of XML file to work. The online demo correctly shows the results from the XML file but when I generate the PHP source code via the demo and try to run that as a PHP script on my web server, I just get a blank page.

MagicParser was previously working very well, however my client (a real estate company) has recently switched software providers and therefore I have to edit the coding to fit in with a new XML 'style'.

I can't get my head around why the demo code isn't working for me. Here is the code below:

<?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["RENTAL"];
    print $record["RENTAL-MODTIME"];
    print $record["RENTAL-STATUS"];
    print $record["AGENTID"];
    print $record["UNIQUEID"];
    print $record["LISTINGAGENT"];
    print $record["LISTINGAGENT/NAME"];
    print $record["LISTINGAGENT/TELEPHONE"];
    print $record["LISTINGAGENT/TELEPHONE-TYPE"];
    print $record["LISTINGAGENT/EMAIL"];
    print $record["DATEAVAILABLE"];
    print $record["RENT"];
    print $record["RENT-PERIOD"];
    print $record["ADDRESS"];
    print $record["ADDRESS-DISPLAY"];
    print $record["ADDRESS/STREETNUMBER"];
    print $record["ADDRESS/STREET"];
    print $record["ADDRESS/SUBURB"];
    print $record["ADDRESS/POSTCODE"];
    print $record["ADDRESS/COUNTRY"];
    print $record["CATEGORY"];
    print $record["CATEGORY-NAME"];
    print $record["HEADLINE"];
    print $record["DESCRIPTION"];
    print $record["FEATURES"];
    print $record["FEATURES/BEDROOMS"];
    print $record["FEATURES/BATHROOMS"];
    print $record["FEATURES/CARPORTS"];
    print $record["HOLIDAY"];
    print $record["HOLIDAY-VALUE"];
    print $record["OBJECTS"];
    print $record["OBJECTS/IMG"];
    print $record["OBJECTS/IMG-ID"];
    print $record["OBJECTS/IMG-MODTIME"];
    print $record["OBJECTS/IMG-FILE"];
  }
  
  MagicParser_parse("www.harveysdunedin.co.nz/console_19-02-2009_15-39-53.zip","myRecordHandler","xml|PROPERTYLIST/RENTAL/");
?>

Not too sure if this could be do to with the ZIP filetype rather than XML, as I've tried just using the XML file from within the ZIP and that didn't do the trick either.

What am I doing wrong?

Submitted by support on Sun, 2009-02-22 08:56

Hi There,

Yes - it will be the .zip format; as whilst the demo script will check for uploaded compressed
files, this is not a feature of Magic Parser itself, which requires a plain text XML stream.

Of course one option is to download the file to your local computer, unzip (using Winzip or
similar) and then upload and parse the uncompressed XML as a local file.

However, if you would like to try the same method that the demo script uses, here's what you
would need to do.

Firstly, you need to create a writable directory into which you can download the remote file
and then unzip it. I would recommend a directory called "data" in the same folder as your
main script. An easy way to make sure that the directory is writeable by PHP is to make
it writeable by all users (owner/group/world) using your FTP program (which you can probably
use to create the folder also). Right click on the folder and look for the Permissions... or
perhaps "Properties" then "Permissions..", and give write access to all users.

With that in place, the following code may work if your PHP installation has access to the
wget and unzip commands:

$cmd = "wget -O \"data/console.zip\" \"http://www.harveysdunedin.co.nz/console_19-02-2009_15-39-53.zip\"";
exec($cmd);
$cmd = "unzip -p \"data/console.zip\" > \"data/console.xml\"";
exec($cmd);
MagicParser_parse("data/console.xml","myRecordHandler","xml|PROPERTYLIST/RENTAL/");

If that doesn't work, first try adding a path to the commands, for example /usr/bin/wget
and /usr/bin/unzip which are the most likely locations - this would only be necessary if
PHP does not have a path variable set...

Hope this helps!
Cheers,
David.

Submitted by Benjip on Sun, 2009-02-22 22:24

Thanks David. Am hoping that will do the trick (not at my own computer currently so can't give it a test); didn't realise that the demo was doing something different with the ZIP file!

One other slightly related question - how would I use the above wget & unzip code if I wanted a script to automatically use the 'most recent' ZIP file in the /data/ directory?

For example if a ZIP file was uploaded at 9:07am and I have a cronjob of a script going every hour, how would I make the script (which would next run at 10am) get the most recent ZIP file and do its thing? The files are named after the date and time so it could even be the filename rather than the 'date modified'.

Hopefully that makes sense!

Submitted by support on Mon, 2009-02-23 09:16

Hi,

What I would do is never keep the .zip file in the /data/ folder - once it us unzipped to your common filename (e.g. console.xml) you would delete the zip file, for example:

$cmd = "unlink \"data/console.xml\"";
unlink($cmd);

However, I think you mean that you want to request the latest ZIP file from the remote server? So this means that you have to derive the filename. You could try this using PHP's date() function, which lets you create a string containing parts of the current date / time. For example, in place of:

$cmd = "wget -O \"data/console.zip\" \"http://www.harveysdunedin.co.nz/console_19-02-2009_15-39-53.zip\"";
exec($cmd);

...you could try something like:

$date = date("d-m-Y_H-i-s");
$url = "http://www.harveysdunedin.co.nz/console_".$date.".zip";
$cmd = "wget -O \"data/console.zip\" \"\"";
exec($cmd);

Of course, this relies on the remote file being guaranteed to be there up to the second, and for both clocks to be accurate..!

Hope this helps,
Cheers,
David.