前回のエントリの続きですが、Zend AMF を使用して画像のバイナリデータを取得するときの注意事項は、以下の 2 点です。
- AMF3 形式で通信
- Zend_Amf_Value_ByteArray API で、AS3 の ByteArray 型に変換
例 :
service.php
<?php
require_once 'Zend/Amf/Server.php';
require_once 'Zend/Amf/Value/ByteArray.php';
$server = new Zend_Amf_Server();
$server->setClass('PHPService');
$server->setClassMap('TestResultVO', 'TestResult');
$response = $server->handle();
echo $response;
class PHPService {
public function test($args) {
$filename = 'hoge.jpg';
$file = fopen($filename,'rb');
$content = fread($file, filesize($filename));
fclose($file);
$result = new TestResult();
$result->bytes = new Zend_Amf_Value_ByteArray($content);
return $result;
}
}
class TestResult {
public $bytes;
}
?>
Test.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx = "http://ns.adobe.com/mxml/2009"
xmlns:s = "library://ns.adobe.com/flex/spark"
applicationComplete = "test()"
>
<fx:Script>
<![CDATA[
protected function test():void {
var responder:Responder = new Responder(onResult, onFault);
var connection:NetConnection = new NetConnection();
connection.objectEncoding = ObjectEncoding.AMF3;
connection.connect("http://*****/service.php");
connection.call("PHPService.test", responder, []);
}
protected function onResult(result:Object):void {
var data:TestResultVO = TestResultVO(result);
image.source = data.bytes;
}
protected function onFault(fault:Object):void {
}
]]>
</fx:Script>
<s:Image id="image" />
</s:Application>
TestResultVO.as
package {
import flash.utils.ByteArray;
[RemoteClass(alias="TestResultVO")]
public class TestResultVO {
public var bytes :ByteArray;
public function TestResultVO() {}
}
}