You are here:  » Multiple handlers


Multiple handlers

Submitted by vastor on Sun, 2007-09-02 18:30 in

I am using a script that would basically include a number of scripts that are using magicparser.

problem is, i would get a parse error (cannot redeclare function handler)

therefore I thought I would use the PHP create_function in order to generate a random name for the handler every time my script tries to include one of the scripts.

yet, i still get parse error (Parse error: syntax error, unexpected '"')

I think it is because of the quotes inside the create_function argument. How can we fix this ?

thanks.

/***************\
*** getHotels
****************/
$method = 'getHotels';
//////////////////////////
require("key.php");
require("MagicParser.php");
$connection = mysql_connect("supremecenter52.com:3306",$username, $password) or die(mysql_error());
$db = mysql_select_db("vastor_xml",$connection) or die ("Couldn't select database.");
//1- TRUNCATE TABLE
$sql = "DELETE FROM $method";
mysql_query($sql);
//2- INSERT NEW DATA
$counter = 0;
$func_name = create_function('$record', '
global $method;
global $counter;
     global $sql;
     $counter++;
     $sql =
      "INSERT INTO $method (address, from, to, city, city_id,class,class_is_estimated,commision,countrycode,currencycode,district,hotel_id,hoteltype_id,
is_closed,latitude,longtitude,maxrate,minrate,name,nr_rooms,preferred,ranking,review_nr,review_score,url,zip)
       VALUES
       (
       '".$record["ADDRESS"]."',
       '".$record["FROM"]."',
       '".$record["TO"]."',
       '".$record["CITY"]."',
       '".$record["CITY_ID"]."',
       '".$record["CLASS"]."',
       '".$record["CLASS_IS_ESTIMATED"]."',
       '".$record["COMMISION"]."',
       '".$record["COUNTRYCODE"]."',
       '".$record["CURRENCYCODE"]."',
       '".$record["DISTRICT"]."',
       '".$record["HOTEL_ID"]."',
       '".$record["HOTELTYPE_ID"]."',
       '".$record["IS_CLOSED"]."',
       '".$record["LATITUDE"]."',
       '".$record["LONGTITUDE"]."',
       '".$record["MAXRATE"]."',
       '".$record["MINRATE"]."',
       '".$record["NAME"]."',
       '".$record["NR_ROOMS"]."',
       '".$record["PREFERRED"]."',
       '".$record["RANKING"]."',
       '".$record["REVIEW_NR"]."',
       '".$record["REVIEW_SCORE"]."',
       '".$record["URL"]."',
       '".$record["ZIP"]."'
       )
      ";
  mysql_query($sql);
')
$url = "http://$b_user:$b_pass@distribution-xml.booking.com/xml/bookings.$method";
MagicParser_parse($url,$func_name, "xml|$method/result ");
print "<p>Processed ".$counter." records.</p>";
print "<p>Last SQL statement was: ".$sql."</p>";
print mysql_error();

Submitted by support on Sun, 2007-09-02 18:35

Hi,

In general, there's no problem with multiple handlers. The problem may simply be that you are require()'ing MagicParser.php in each of your included files, which is unnecessary.

If you have one single call to require("MagicParser.php"); in your top level script (the one that includes each of the different files), it should work fine as long as the record handler function has a different name within each one.

Hope this helps,
Cheers,
David.

Submitted by vastor on Sun, 2007-09-02 18:46

Okay, will change the names in the scripts.

i posted a question last night about string format, but its not showing in the forum. i will post it again:

I have the following xml structure and I am not sure how to input its format string for magicparser function.

<getHotels>
<result>
  <address></address>
  <checkin>
      <from/>
      <to></to>
  </checkin>
  <checkout>
     <from></from>
     <to></to>
  </checkout>
  <city></city>
  <city_id></city_id>
  <class></class>
  <class_is_estimated></class_is_estimated>
  <commission></commission>
  <countrycode></countrycode>
  <currencycode></currencycode>
  <district/>
  <hotel_id></hotel_id>
  <hoteltype_id></hoteltype_id>
  <is_closed></is_closed>
  <location>
     <latitude></latitude>
     <longitude></longitude>
  </location>
  <maxrate></maxrate>
  <minrate></minrate>
  <name></name>
  <nr_rooms></nr_rooms>
  <preferred></preferred>
  <ranking></ranking>
  <review_nr></review_nr>
  <review_score></review_score>
  <url></url>
  <zip></zip>
</result>
</getHotels>

Submitted by support on Sun, 2007-09-02 18:50

I'm so sorry that you post last night didn't appear - I'll investigate that.

The format string you would need to use with the XML posted above (i'm assuming that auto-detection fails because it has only a single record) would be:

xml|GETHOTELS/RESULT/

That should do the trick,
Cheers,
David.

Submitted by vastor on Sun, 2007-09-02 19:58

Hi david, i tried that format before i post the question, it didnt work and neither did the getFormat function, I think its because of the child objects in the xml source. has and and so does . we have 2 sets of and here.

help ;/

this is the php script am using:

function getHotels($record)
  {
   global $method;
global $counter;
     global $sql;
     $counter++;
     $sql =
      "INSERT INTO $method (address, checkin_from, checkin_to, checkout_from , checkout_to , city, city_id,class,class_is_estimated,commision,countrycode,currencycode,district,hotel_id,hoteltype_id,
is_closed,latitude,longtitude,maxrate,minrate,name,nr_rooms,preferred,ranking,review_nr,review_score,url,zip)
       VALUES
       (
       '".$record["ADDRESS"]."',
       '".$record["FROM"]."',
       '".$record["TO"]."',
       '".$record["FROM"]."',
       '".$record["TO"]."',
       '".$record["CITY"]."',
       '".$record["CITY_ID"]."',
       '".$record["CLASS"]."',
       '".$record["CLASS_IS_ESTIMATED"]."',
       '".$record["COMMISION"]."',
       '".$record["COUNTRYCODE"]."',
       '".$record["CURRENCYCODE"]."',
       '".$record["DISTRICT"]."',
       '".$record["HOTEL_ID"]."',
       '".$record["HOTELTYPE_ID"]."',
       '".$record["IS_CLOSED"]."',
       '".$record["LATITUDE"]."',
       '".$record["LONGTITUDE"]."',
       '".$record["MAXRATE"]."',
       '".$record["MINRATE"]."',
       '".$record["NAME"]."',
       '".$record["NR_ROOMS"]."',
       '".$record["PREFERRED"]."',
       '".$record["RANKING"]."',
       '".$record["REVIEW_NR"]."',
       '".$record["REVIEW_SCORE"]."',
       '".$record["URL"]."',
       '".$record["ZIP"]."'
       )
      ";
  mysql_query($sql);
}
$url = "http://$b_user:$b_pass@distribution-xml.booking.com/xml/bookings.$method";
MagicParser_parse($url,"getHotels", "xml|$method/result ");

Submitted by support on Sun, 2007-09-02 20:03

Hi,

Child objects shouldn't cause any problem. For example, the latitude should be in the record as:

$record["LOCATION/LATITUDE"]

Could you possibly email me a copy of the XML document you are working with and I'll check it out for you. Reply to you reg code or forum registration email is the easiest way to get me...

Cheers,
David.

Submitted by vastor on Sun, 2007-09-02 20:40

Hi David,

whats your email? support@iaaisoftware.com ?

meanwhile, the xml am working with is on a remote server, I am pasting a few records here: (please ignore the browser (mozilla) -'s)

I keep getting: Premature end of script headers: getHotels.php


<getHotels>

<result>
<address>No. 69 Fuxi Street</address>

<checkin>
<from/>
<to>24/24</to>
</checkin>

<checkout>
<from>07:00</from>
<to>12:00</to>
</checkout>
<city>Taiyuan (Shanxi/Asia/China)</city>
<city_id>-3830403</city_id>
<class>5</class>
<class_is_estimated>0</class_is_estimated>
<commission>0</commission>
<countrycode>cn</countrycode>
<currencycode>USD</currencycode>
<district/>
<hotel_id>67343</hotel_id>
<hoteltype_id>14</hoteltype_id>
<is_closed>0</is_closed>

<location>
<latitude>30.698826137188</latitude>
<longitude>118.418448784591</longitude>
</location>
<maxrate>206</maxrate>
<minrate>94</minrate>
<name>Jin Jiang World Trade Hotel</name>
<nr_rooms>398</nr_rooms>
<preferred>0</preferred>
<ranking>12761</ranking>
<review_nr>0</review_nr>
<review_score>0</review_score>

<url>
http://www.booking.com/hotel/cn/jin-jiang-world-trade.html
</url>
<zip>030002</zip>
</result>

<result>
<address>Calabash Bay Treasure Beach</address>

<checkin>
<from>15:00</from>
<to>23:00</to>
</checkin>

<checkout>
<from>12:00</from>
<to>12:00</to>
</checkout>
<city>St Elizabeth (Jamaica)</city>
<city_id>-3754069</city_id>
<class>0</class>
<class_is_estimated>0</class_is_estimated>
<commission>0</commission>
<countrycode>jm</countrycode>
<currencycode>USD</currencycode>
<district/>
<hotel_id>88056</hotel_id>
<hoteltype_id>14</hoteltype_id>
<is_closed>0</is_closed>

<location>
<latitude>17.8733412147694</latitude>
<longitude>-77.7604469466189</longitude>
</location>
<maxrate>395</maxrate>
<minrate>95</minrate>
<name>Jake's Hotel</name>
<nr_rooms>36</nr_rooms>
<preferred>0</preferred>
<ranking>5437</ranking>
<review_nr>0</review_nr>
<review_score>0</review_score>
<url>http://www.booking.com/hotel/jm/jake-s.html</url>
<zip/>
</result>

and here is the full PHP script:

<?PHP
// - Part of the XML MYSQL INSERTION INTERFACE
//
/***************\
*** getHotels
****************/
$method = 'getHotels';
//////////////////////////
require("key.php");
require("MagicParser.php");
$connection = mysql_connect("supremecenter52.com:3306",$username, $password) or die(mysql_error());
$db = mysql_select_db("vastor_xml",$connection) or die ("Couldn't select database.");
//1- TRUNCATE TABLE
$sql = "DELETE FROM $method";
mysql_query($sql);
//2- INSERT NEW DATA
$counter = 0;
function getHotels($record)
  {
   global $method;
global $counter;
     global $sql;
     $counter++;
     $sql =
      "INSERT INTO $method (address, checkin_from, checkin_to, checkout_from , checkout_to , city, city_id,class,class_is_estimated,commision,countrycode,currencycode,district,hotel_id,hoteltype_id,
is_closed,latitude,longtitude,maxrate,minrate,name,nr_rooms,preferred,ranking,review_nr,review_score,url,zip)
       VALUES
       (
       '".$record["ADDRESS"]."',
       '".$record["CHECKIN/FROM"]."',
       '".$record["CHECKIN/TO"]."',
       '".$record["CHECKOUT/FROM"]."',
       '".$record["CHECKOUT/TO"]."',
       '".$record["CITY"]."',
       '".$record["CITY_ID"]."',
       '".$record["CLASS"]."',
       '".$record["CLASS_IS_ESTIMATED"]."',
       '".$record["COMMISION"]."',
       '".$record["COUNTRYCODE"]."',
       '".$record["CURRENCYCODE"]."',
       '".$record["DISTRICT"]."',
       '".$record["HOTEL_ID"]."',
       '".$record["HOTELTYPE_ID"]."',
       '".$record["IS_CLOSED"]."',
       '".$record["LOCATION/LATITUDE"]."',
       '".$record["LOCATION/LONGTITUDE"]."',
       '".$record["MAXRATE"]."',
       '".$record["MINRATE"]."',
       '".$record["NAME"]."',
       '".$record["NR_ROOMS"]."',
       '".$record["PREFERRED"]."',
       '".$record["RANKING"]."',
       '".$record["REVIEW_NR"]."',
       '".$record["REVIEW_SCORE"]."',
       '".$record["URL"]."',
       '".$record["ZIP"]."'
       )
      ";
  mysql_query($sql);
}
$url = "http://$b_user:$b_pass@distribution-xml.booking.com/xml/bookings.$method";
MagicParser_parse($url,"getHotels", "xml|$method/result ");
print "<p>Processed ".$counter." records.</p>";
print "<p>Last SQL statement was: ".$sql."</p>";
print mysql_error();
?>

Submitted by support on Sun, 2007-09-02 20:42

Hi,

Yes - if you could email the file as an attachment to support@iaaisoftware.com i'll take a look for you. Make sure that you save the actual XML source rather than a copy from the browser as both Firefox and IE don't display the actual XML when you view an XML document. You can use the "Save File As..." or View Source then Save As... to save the actual XML...

Cheers,
David.