[Zend_Amf + FLEX] Passage d'un objet Flex->PHP
Bonjour,
Je mets d'abord le code:
Le PHP:
service.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
require_once('Zend/Amf/Server.php');
require_once('MyService.php');
$server = new Zend_Amf_Server();
//adding our class to Zend AMF Server
$server->setClass("MyService");
//Mapping the ActionScript VO to the PHP VO
//you don't have to add the package name
$server->setClassMap("MyData", "MyData");
$server->setClassMap("MyDataCollection", "MyDataCollection");
echo($server -> handle()); |
MyService.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| require_once('MyData.php');
require_once('MyDataCollection.php');
class MyService {
public function getData() {
$ret = array();
$myDataCollection = new MyDataCollection();
$tmp = new MyData((int) rand(1, 9), "chaine1-".((int) rand(1, 9)));
$myDataCollection->myArray[] = $tmp;
$tmp = new MyData((int) rand(1, 9), "chaine1-".((int) rand(1, 9)));
$myDataCollection->myArray[] = $tmp;
$ret[] = $myDataCollection;
$myDataCollection = new MyDataCollection();
$tmp = new MyData((int) rand(1, 9), "chaine2-".((int) rand(1, 9)));
$myDataCollection->myArray[] = $tmp;
$tmp = new MyData((int) rand(1, 9), "chaine2-".((int) rand(1, 9)));
$myDataCollection->myArray[] = $tmp;
$ret[] = $myDataCollection;
return $ret;
}
public function setData($myData) {
$handle = fopen("test.txt", 'w');
fwrite($handle, $myData->toString()."\r\n");
fclose($handle);
return NULL;
}
} |
MyData.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| class MyData {
public $myInt;
public $myString;
public function __construct($myInt, $myString) {
$this->myInt = $myInt;
$this->myString = $myString;
}
public function toString() {
return $this->myInt . " - " . $this->myString;
}
} |
MyDataCollection.php
Code:
1 2 3 4
| class MyDataCollection {
public $myArray;
} |
L'actionscript:
test_amf.mxml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import my_classes.MyData;
import my_classes.MyDataCollection;
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
[Bindable]
public var myData:MyData;
[Bindable]
public var myDataCollection:MyDataCollection;
private function getDataListener(event:ResultEvent):void {
var arrayTemp:Array = event.result as Array;
for each(myDataCollection in arrayTemp) {
for each(myData in myDataCollection.myArray) {
myTextArea.text += myData + '\n';
}
}
myTextArea.text += '\n';
}
private function setDataListener(event:ResultEvent):void {
myTextArea.text += "Données envoyées\n\n";
}
private function faultListener(event:FaultEvent):void {
Alert.show(event.fault.message, "Error");
}
]]>
</mx:Script>
<mx:RemoteObject id="myRemote" destination="zend" source="MyService" showBusyCursor="true" fault="faultListener(event)">
<mx:method name="getData" result="getDataListener(event)"/>
<mx:method name="setData" result="setDataListener(event)"/>
</mx:RemoteObject>
<mx:VBox top="30" left="100">
<mx:Button label="Get Data" click="{myRemote.getData()}"/>
<mx:Button label="Set Data" click="{myRemote.setData(myDataCollection)}"/>
<mx:TextArea id="myTextArea" text="" height="400" width="400"/>
<mx:HBox>
<mx:Label text="myData"/>
<mx:TextInput text="{myData}"/>
</mx:HBox>
<mx:HBox>
<mx:Label text="myDataCollection"/>
<mx:DataGrid dataProvider="{myDataCollection.myArray}"/>
</mx:HBox>
</mx:VBox>
</mx:Application> |
MyData.as
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package my_classes
{
[RemoteClass(alias="MyData")]
[Bindable]
public class MyData
{
public var myInt:int;
public var myString:String;
public function MyData() {
this.myInt = 10;
this.myString = "chaine test";
}
public function toString():String {
return myInt + " - " + myString;
}
}
} |
MyDataCollection
Code:
1 2 3 4 5 6 7 8 9
| package my_classes
{
[RemoteClass(alias="MyDataCollection")]
[Bindable]
public class MyDataCollection
{
public var myArray:Array;
}
} |
Lorsque j'appelle la fonction setData(myDataCollection), j'ai l'erreur suivante :
Citation:
faultCode:Client.Error.DeliveryInDoubt
faultString:'Channel disconnected'
faultDetail:'Channel disconnected before an acknowledgement was received'
Or coté PHP, le contenu de myDataCollection est bien écrit dans le fichier test.txt.
Je galère depuis 2 jours dessus,
Aidez moi !!!
Merci