AMF 类型对象从 flex 返回到 PHP [英] AMF typed objects from flex back to PHP

查看:21
本文介绍了AMF 类型对象从 flex 返回到 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,我成功地使用 Zend_AMF 将类型化对象从 PHP 应用程序发送到 flex 前端,按照此 问题.

Yesterday, I succeeded in sending typed objects from a PHP application to a flex front-end using Zend_AMF as per this question.

我现在面临的问题是我想从 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\CTestmodules.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.

但是我怎样才能在 php 命名空间中使用它呢?

But how can I get this working with php namespaces?

推荐答案

终于解决了这个问题.这是为将来可能遇到相同问题的人提供的解决方案.

Finally resolved the issue. Here is the solution for those who might come across the same issue in the future.

对于您的 PHP 类:

For your PHP class:

<?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);
  }

if(!$class) {
     $class = str_replace('.', '\\', $className);
}

实际上,我们可以只对类映射使用点语法,它可以很好地放置在 actionscript 中:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆