2010-01-17 16 views
2

Flash + AMFPHP是一個很好的組合。但是,有些情況下,由於各種原因,使用NetConnection的Flash Remoting不是正確的工具。羅布對這個偉大的職位前段時間:http://www.roboncode.com/articles/144AMFPHP:通過HTTP序列化Flash對象,無需網關

他還對如何提供AMF到一個http請求一個很好的例子,沒有POST和AMF請求包來調用的NetConnection發送功能,採用Zend_AMF 。

// Include the Zend Loader 
include_once 'Zend/Loader.php'; 
// Tell the Zend Loader to autoload any classes we need 
// from the Zend Framework AMF package 
Zend_Loader::registerAutoload(); 

// Create a simple data structure 
$data = array('message' => 'Hello, world!'); 
// Create an instance of an AMF Output Stream 
$out = new Zend_Amf_Parse_OutputStream(); 
// We will serialize our content into AMF3 for this example 
// You could alternatively serialize it as AMF0 for legacy 
// Flash applications. 
$s = new Zend_Amf_Parse_Amf3_Serializer($out); 
$s->writeObject($data); 

// Return the content (we have found the newline is needed 
// in order to process the data correctly on the client side) 
echo "\n" . $out->getStream(); 

我非常喜歡這種方法,並且非常喜歡用AMFPHP進行復制。爲什麼選擇AMFPHP? '最新'版本使用amf-ext(C PHP擴展)來序列化和反序列化數據。它比ZendAMF仍在使用的php方式快得多。

當然,我已經玩過AMFPHP,並嘗試構建必要的對象並使用Serializer類。我甚至得到了一個有效的AMF字符串,但真正的數據總是被一個'方法包'包裝,告訴接收方這是'Service.method'調用的答案。

那麼有沒有辦法在AMFPHP中直接序列化Flash對象,而不使用網關和方法包裝?

謝謝。

回答

4

好吧,它現在就開始工作。

這比Zend_AMF解決方案稍微複雜一些,但要快得多。這裏是我的代碼:

$data = array('message' => 'Hello, world!'); 

// Create the gateway and configure it 
$amf = new Gateway(); 
Amf_Server::$encoding = 'amf3'; 
Amf_Server::$disableDebug = true; 

// Construct a body 
$body = new MessageBody("...", "/1", array()); 
$body->setResults($data); 
$body->responseURI = $body->responseIndex . "..."; 

// Create the object and add the body 
$out = new AMFObject(); 
$out->addBody($body); 

// Get a serializer and use it 
$serializer = new AMFSimpleSerializer(); 
$result = $serializer->serialize($out); 

正如你看到有一個新的類AMFSimpleSerializer我建:

class AMFSimpleSerializer extends AMFSerializer 
{ 
    function serialize(&$amfout) 
    { 
     $encodeCallback = array(&$this,"encodeCallback"); 

     $body = &$amfout->getBodyAt(0); 

     $this->outBuffer = ""; 
     $this->outBuffer .= amf_encode($body->getResults(), $this->encodeFlags, $encodeCallback); 
     $this->outBuffer = substr($this->outBuffer, 1); 

     return $this->outBuffer; 
    } 
} 

如果安裝amfext這個類只有工作,但很容易被改裝成使用PHP enocding過程。我沒有實現它,因爲我在AMFPHP的重大修改版本上構建了它。

我希望我將代碼中的所有類替換爲實際的AMFPHP對應類。我會盡量明天測試這個,並在必要時更新這個答案。

完成後,我發現AMFPHP幾乎沒有任何東西留在課堂上,它只是調用amf_encode並刪除第一個字節,以便客戶端可以理解他得到的東西。

簡單,簡單,快速。

1

這是不需要的簡化版本amfext:

require_once('amfphp/core/amf/app/Gateway.php'); 
require_once(AMFPHP_BASE . 'amf/io/AMFSerializer.php'); 

$data = array('message' => 'Hello, world!') 

$serializer = new AMFSerializer(); 
$serializer->writeAmf3Data($data); 

print $serializer->outBuffer; 

沒有換行和必要的字符串。 AMFPHP 1.9,Flex 3.4。

相關問題