将AMF类型的对象从flex转换回PHP [英] AMF typed objects from flex back to PHP
问题描述
昨天,按照此问题。
我现在面临的问题是我想将类型对象从flex发送到PHP,在PHP方面,可以将其作为类型对象而不是 stdClass
。
The problem I am facing now is that I would like to send a typed object from flex to PHP and on the PHP side, be able to recieve it as a typed object instead of stdClass
.
这里是类在flex中:
Here is the class in flex:
package com.mysite
{
[Bindable]
[RemoteClass(alias="CTest")]
public class CTest
{
public var stuff1:String;
public var stuff2:String;
public function CTest()
{
}
}
}
这是PHP中的相应类:
And this is the corresponding class in PHP:
<?php
namespace modules\testing;
class CTest{
var $_explicitType = 'CTest';
var $stuff1;
var $stuff2;
}
在flex中,我像这样发送对象:
In flex, I am sending the object like so:
var remoteObject:RemoteObject = new RemoteObject();
remoteObject.endpoint = "http://localhost/to/my/amf/endpoint";
remoteObject.showBusyCursor = true;
remoteObject.source = 'testing';
var op:AbstractOperation = remoteObject.getOperation(null);
op.addEventListener(ResultEvent.RESULT, result);
op.send( new CTest());
在PHP方面,对象被检索到名为 $ parameters的变量中
。然后,在其上使用print_r将结果写入文件:
On the PHP side, the object is retrieved into a variable called $parameters
. I then use print_r on it to write the result to a file:
$z = print_r($parameters, true);
$s = fopen('D:\test.txt', 'w+');
fwrite($s, $z);
fclose($s);
可以看出,返回的结果是未键入的,并且是 stdClass
对象:]
And as can be seen, the result comes back untyped and is a stdClass
object:]
Array
(
[0] => stdClass Object
(
[stuff1] =>
[stuff2] =>
)
)
经过一些测试,我从PHP对象中删除了命名空间,并将其移到了全局命名空间中。这似乎已经解决了问题。我尝试将 RemoteClass
设置为 \modules\testing\CTest
以及 modules .testing.CTest
。然后将两个测试的 $ _ eplicitType
设置为相同的值。
After some testing, I removed the namespace from the PHP object and moved it into the global namespace. This seems to have resolved the issue. I have tried setting RemoteClass
to \modules\testing\CTest
and also modules.testing.CTest
. $_eplicitType
was then set to the same value for both tests.
结果是当我使用 modules.testing.CTest
,这是Zend_AMF看到的类名。如果我使用命名空间符号,则Zend_AMF会看到 modulestestingCTest
,因为所有的斜杠都被去除了。
The result is that when I use modules.testing.CTest
, this is the classname that Zend_AMF sees. If I use namespace notation, Zend_AMF sees modulestestingCTest
, as all the slashes get stripped out.
但是我怎么办
推荐答案
最终解决了该问题。这是为将来可能遇到相同问题的人提供的解决方案。
Finally resolved the issue. Here is the solution for those who might come across the same issue in the future.
对于您的PHP类:
<?php
namespace app\testing;
class CTest{
var $_explicitType = 'app\testing\CTest';
var $stuff1;
var $stuff2;
}
对于您的动作脚本类:
package com.mysite
{
[Bindable]
[RemoteClass(alias="app\\\\testing\\\\CTest")]
public class CTest
{
public var stuff1:String;
public var stuff2:String;
public function CTest()
{
}
}
}
我认为问题是由于AMF主体的序列化和反序列化引起的。实际上,远程类有2个斜杠转义了,结果是: app\\testing\\CTest
。一旦它被Zend_AMF反序列化,由于有2个斜杠,这意味着反斜杠被转义为: app\testing\CTest
。当然,这只是预感,但是与Charles进行的一些测试证明确实是这种情况。
I think the problem is due to the serialization and deserialization of the AMF body. Effectively, the remote class is has 2 slashes escaped, resulting in this: app\\testing\\CTest
. Once it is deserialzed by Zend_AMF, since there are 2 slashes, it means that the backslashes are escaped into this: app\testing\CTest
. This is just a hunch of course, but some testing with Charles proved that this is the case.
另一种可能的解决方案是修改 Zend_Amf_Parse_TypeLoader.php
。在第99行,我们可以替换:
Another possible solution is to modify Zend_Amf_Parse_TypeLoader.php
. On line 99, we could replace:
if(!$class) {
$class = str_replace('.', '_', $className);
}
with
if(!$class) {
$class = str_replace('.', '\\', $className);
}
有效地,我们可以将点语法用于类映射,这是有效的会很好地放置在:
Effectively, we can then just use dot syntax for the class mappings, which actionscript will place nicely with:
[RemoteClass(alias="app.testing.CTest")]
var $_explicitType = 'app.testing.CTest';
显然,这是一个很好的解决方案,但我不希望使用供应商代码进行修改,因为Zend_AMF库将来会更新为新版本。现在,我的解决方案是只使用4个斜杠。
Obviously this will is a pretty nice solution, but I prefer not to modify with vendor code, as the Zend_AMF library will be updated with newer versions in the future. For now, my solution is to just use 4 slashes.
这篇关于将AMF类型的对象从flex转换回PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!