2013-07-20 47 views
4

我想用PHPRETS下载RETS FEED数据和图像,我成功下载了CSV数据,但是,虽然图像是在图像文件夹中正确创建的,但每个图像的大小是零。我附上我在这里使用的代码,请帮助我,以便我可以正确下载图像。使用phprets从rets feed下载图像

<?php 

    $rets_login_url = "http://retsgw.flexmls.com:80/rets2_0/Login"; 
    $rets_username = "**********"; 
    $rets_password = "**********"; 

    // use http://retsmd.com to help determine the SystemName of the DateTime field which 
    // designates when a record was last modified 
    $rets_modtimestamp_field = "LIST_87"; 

    // use http://retsmd.com to help determine the names of the classes you want to pull. 
    // these might be something like RE_1, RES, RESI, 1, etc. 
    $property_classes = array("B"); 

    // DateTime which is used to determine how far back to retrieve records. 
    // using a really old date so we can get everything 
    $previous_start_time = "1980-01-01T00:00:00";  require_once("lib/phrets.php"); 

    // start rets connection 
    $rets = new phRETS; 

    // only enable this if you know the server supports the optional RETS feature called 'Offset' 
    $rets->SetParam("offset_support", true); 

    echo "+ Connecting to {$rets_login_url} as {$rets_username}<br>\n"; 
    $connect = $rets->Connect($rets_login_url, $rets_username, $rets_password); 

    if ($connect) { 
    echo " + Connected<br>\n"; 
    } 
    else { 
     echo " + Not connected:<br>\n"; 
    print_r($rets->Error()); 
    exit; 
    } 

    foreach ($property_classes as $class) { 

    echo "+ Property:{$class}<br>\n"; 

    $file_name = strtolower("property_{$class}.csv"); 
    $fh = fopen($file_name, "w+") or die("Can't open file"); 

    $fields_order = array(); 

    $query = "({$rets_modtimestamp_field}={$previous_start_time}+)"; 

    // run RETS search 
    echo " + Resource: Property Class: {$class} Query: {$query}<br>\n"; 
    $search = $rets->SearchQuery("Property", $class, $query, array('Limit' => 1000)); 
    $get_id = $_REQUEST['LIST_1']; 
    if ($rets->NumRows($search) > 0) { 

      // print filename headers as first line 
      $fields_order = $rets->SearchGetFields($search); 
      fputcsv($fh, $fields_order); 

      // process results 
      while ($record = $rets->FetchRow($search)) { 
        $this_record = array(); 
        foreach ($fields_order as $fo) { 

    if ($fo == 'LIST_1') { 

     $photos = $rets->GetObject("Property", "Photo", $record[$fo], "*", 1); 

     foreach ($photos as $photo) { 

      if ($photo['Success'] == true) { 

       file_put_contents("photos/{$photo['Content-ID']}-{$photo['Object-ID']}.jpg", $photo['Data']); 

      } 
     } 
    } 
    $this_record[] = $record[$fo]; 
}       
    fputcsv($fh, $this_record);  
      } 

    } 

    echo " + Total found: {$rets->TotalRecordsFound($search)}<br>\n"; 

    $rets->FreeResult($search); 

    fclose($fh); 

    echo " - done<br>\n"; 

    } 

    echo "+ Disconnecting<br>\n"; 
    $rets->Disconnect(); 
    ?> 

回答

6

你有这条线。它返回的是图像URL,而不是二进制图像数据。

$photos = $rets->GetObject("Property", "Photo", $record[$fo], "*", 1); 

第五个参数应该是0。

$photos = $rets->GetObject("Property", "Photo", $record[$fo], "*", 0); 

按照PHRETS文档中,1分返回图像URL和0返回二值图像数据。

https://github.com/troydavisson/PHRETS/wiki/GetObject

+0

Thanks..it为我工作! – user38427

+2

此外,图片网址将不会被所有RETS支持。因此,如果您出于某种原因需要URL,请确保公共URL支持由RETS提供。 –