我在网上找到了这段代码,它将连接到 rets 提要并创建 mysql 表。
<?php
/* Raw example on how to use phrets to generate the necessary sql code
for mysql tables generation from rets.
Works for me*/
require ('phrets0.6.1.php');
$rets = new phRETS;
$Host = 'http://demo.crt.realtors.org:6103/rets/login';
$Account = 'Joe';
$Password = 'Schmoe';
$User_Agent = 'RETS_class/1.0';
$Rets_Version = 'RETS/1.5' ;
$rets->AddHeader("Accept", "*/*");
$rets->AddHeader("RETS-Version", "$Rets_Version");
$rets->AddHeader("User-Agent", "$User_Agent");
$rets->SetParam("cookie_file", "phrets_cookies.txt");
//$rets->SetParam("debug_mode", TRUE); // ends up in rets_debug.txt
$rets->Connect($Host,$Account,$Password);
$ResourcesInfo1 = $rets->GetMetadataInfo();
$MetadataInfo1 = $rets->GetMetadataTypes();
/*testing4GetTables*/
function GetTables($ResourcesInfo, $MetadataInfo) {
/* Put toghether all the rets info needed for database tables creation
in an array. */
foreach ($MetadataInfo as $key => $value){
$Resource = $value['Resource'];
foreach ($value['Data'] as $key){
$tables[$key['Description']] = array(
'ResourceID' => $Resource,
'Description' => $key['Description'],
'ClassName' => $key['ClassName'] ,
'KeyField' => $ResourcesInfo[$Resource]['KeyField'],
'ClassCount' => $ResourcesInfo[$Resource]['ClassCount']
);
}
}
return $tables;
}
function CreateMysqlTables ($ResourcesInfo, $MetadataInfo, $class){
/*function providing the mysql code needed to create mysqltables.
to be run as the script on the commande line for nicer output*/
$table = GetTables($ResourcesInfo, $MetadataInfo);
foreach ($table as $key => $value){
$TablesAndFields[$value['Description']] = $class-
>GetMetadata($value['ResourceID'],$value['ClassName']);
}
foreach ($TablesAndFields as $key => $value){
$find = array ('/ /', '/-/');
$TableName = preg_replace($find, "_", $key);
echo "\n\r CREATE TABLE IF NOT EXISTS $TableName (\n\r";
foreach ( $value as $key2 => $value2){
echo "`$value2[SystemName]` ";
ConvertTypeToMysql($value2[DataType], $value2[MaximumLength]);
}
echo "`MyNewTablesTimeStamp` timestamp NOT NULL default
CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,\n\r";
echo " PRIMARY KEY (".$table[$key]['KeyField']."));\n\r";
}
}
function ConvertTypeToMysql($datatype, $datalenght){
/* This function to convert mls datatype and datalenght to mysql.
Change to fit your needs works for me: db2 to mysql (flexmls)*/
switch ($datatype){
case "Character":
if ($datalenght >= 255){
echo "text collate utf8_unicode_ci default NULL, \n\r";
}
elseif (!$datalenght){
echo "$datatype(25) collate utf8_unicode_ci default NULL, \n\r";
}
else {
echo "$datatype($datalenght) collate utf8_unicode_ci default NULL,
\n\r";
}
break;
case "Decimal":
echo "$datatype($datalenght,0) default NULL, \n\r";
break;
case "Int":
echo "$datatype($datalenght) default NULL, \n\r";
break;
case "Long":
/* note:if I put this case after case ("Date" or "Boolean"): it won't
work, Why? is Long a Boolean?'*/
echo "LONGTEXT collate utf8_unicode_ci default NULL, \n\r";
break;
case ("Date" or "Boolean"):
echo "$datatype default NULL, \n\r";
break;
}
}
CreateMysqlTables ($ResourcesInfo1, $MetadataInfo1, $rets);
$rets->Disconnect();
?>
对不起,很长的一段代码。
我已经使用我的 IDE 通过 FTP 建立了与我的 Wordpress 站点的连接,并在那里加载了 PHrets 文件。
我的最终目标是使用 rets 提要中的所有列表填充 mysql 数据库,然后让我的网站在数据库每隔一段时间更新一次以检查更新或新列表时工作。
如果有人能指出我正确的方向,我将不胜感激。