Home  >  Article  >  Backend Development  >  php封装amf数据,发送post

php封装amf数据,发送post

WBOY
WBOYOriginal
2016-06-23 14:22:461003browse

模拟客户端请求,AMF请求 封包,目前只有C#版本的,我没学过C#,想把它翻译成php版本的,但是非常蛋疼啊。想问下目前网上有类似功能的类库了吗?或者已经有相同功能的代码了吗?

class AMF_Post_Data     {         public List<byte> message;          /// <summary>         /// 初始化Message         /// </summary>         /// <param name="at"></param>         /// <param name="headers"></param>         /// <param name="bodies"></param>         public AMF_Post_Data(AMFType at, int headers, int bodies)         {             //AMF版本             if (at == AMFType.AMF0)             {                 message = new List<byte>(new byte[] { 0x00, 0x00 });             }             else if (at == AMFType.AMF3)             {                 message = new List<byte>(new byte[] { 0x00, 0x03 });             }              //header数量             message.Add(BitConverter.GetBytes(headers)[1]);             message.Add(BitConverter.GetBytes(headers)[0]);             //body数量             message.Add(BitConverter.GetBytes(bodies)[1]);             message.Add(BitConverter.GetBytes(bodies)[0]);         }          /// <summary>         /// 添加Target         /// </summary>         /// <param name="target"></param>         /// <param name="Response"></param>         public void AddTargetAndResponse(string target, string Response)         {             //添加Target长度             message.Add(BitConverter.GetBytes(target.Length)[1]);             message.Add(BitConverter.GetBytes(target.Length)[0]);             //添加Target内容             message.AddRange(Encoding.Default.GetBytes(target));              //添加Response长度             message.Add(BitConverter.GetBytes(Response.Length)[1]);             message.Add(BitConverter.GetBytes(Response.Length)[0]);             //添加Response内容             message.AddRange(Encoding.Default.GetBytes(Response));         }          /// <summary>         /// 添加Body         /// </summary>         /// <param name="length"></param>         /// <param name="Content"></param>         public void AddBody(AMF_Post_Data_Body apdb)         {             message.AddRange(apdb.getLength());             message.AddRange(apdb.Content.ToArray());         }     }      class AMF_Post_Data_Body     {         private byte[] length = new byte[4];         public List<byte> Content = new List<byte>();          /// <summary>         /// 初始化Body         /// </summary>         /// <param name="dt"></param>         /// <param name="ArrayLength"></param>         public AMF_Post_Data_Body(DataType dt, int ArrayLength)         {             //添加类型标识             Content.Add((byte)dt);              //数组的话添加长度             if (dt == DataType.Array)             {                 Content.Add(BitConverter.GetBytes(ArrayLength)[3]);                 Content.Add(BitConverter.GetBytes(ArrayLength)[2]);                 Content.Add(BitConverter.GetBytes(ArrayLength)[1]);                 Content.Add(BitConverter.GetBytes(ArrayLength)[0]);             }         }          public void AddData(DataType dt, string value)         {             //添加类型标识             Content.Add((byte)dt);              switch (dt)             {                 case DataType.Number:                     AddData_Number(double.Parse(value));                     break;                 case DataType.String:                     AddData_String(value);                     break;                 case DataType.Boolean:                     AddData_Boolean(Boolean.Parse(value));                     break;             }         }          #region 更种类型处理方法         /// <summary>         /// Boolean         /// </summary>         /// <param name="p"></param>         private void AddData_Boolean(bool p)         {             if (p)                 Content.Add(0x01);             else                 Content.Add(0x00);         }         /// <summary>         /// String         /// </summary>         /// <param name="value"></param>         private void AddData_String(string value)         {             //添加长度             Content.Add(BitConverter.GetBytes(value.Length)[1]);             Content.Add(BitConverter.GetBytes(value.Length)[0]);             //添加内容             Content.AddRange(Encoding.Default.GetBytes(value));         }         /// <summary>         /// Number         /// </summary>         /// <param name="p"></param>         private void AddData_Number(double p)         {             byte[] b = new byte[8];             b = BitConverter.GetBytes(p);             for (int i = 7; i > -1; i--)             {                 Content.Add(b);             }         }         #endregion          public byte[] getLength()         {             length[0] = BitConverter.GetBytes(Content.Count)[3];             length[1] = BitConverter.GetBytes(Content.Count)[2];             length[2] = BitConverter.GetBytes(Content.Count)[1];             length[3] = BitConverter.GetBytes(Content.Count)[0];              return length;         }     }      #region 类型枚举     public enum AMFType     {         AMF0,         AMF3     }      public enum DataType     {         Number = 0,         Boolean = 1,         String = 2,         UntypedObject = 3,         MovieClip = 4,         Null = 5,         Undefined = 6,         ReferencedObject = 7,         MixedArray = 8,         End = 9,         Array = 10,//0x0A         Date = 11,//0x0B         LongString = 12,//0x0C         TypeAsObject = 13,//0x0D         Recordset = 14,//0x0E         Xml = 15,//0x0F         TypedObject = 16,//0x10         AMF3data = 17//0x11     }     #endregion


回复讨论(解决方案)

Amfphp 是PHP的RPC工具,它可以使PHP与下述技术无缝通信:
Flash 和 Flex Remoting
JavaScript JSON 和 Ajax JSON
XML 和XML-RPC

你自己可以搜索一下这个类

至于将 C# 代码移植到 php 也是可以的,不过你需要对两者都熟悉。并且对工作流程也需要很熟悉

Amfphp 是PHP的RPC工具,它可以使PHP与下述技术无缝通信:
Flash 和 Flex Remoting
JavaScript JSON 和 Ajax JSON
XML 和XML-RPC

你自己可以搜索一下这个类

至于将 C# 代码移植到 php 也是可以的,不过你需要对两者都熟悉。并且对工作流程也需要很熟悉

Amfphp 我看过啊。他只是服务端。接收flex或者js或者flash发出的请求。现在我要用php发送请求,不是flex。我不是单单实现rpc才这样做的,是要到网上抓数据。

服务端不是要接收数据吗?
如果是经编码的,不也是要解码的吗?
好了,你倒过来做不就知道什么样的数据开怎么样发送了吗?

服务端不是要接收数据吗?
如果是经编码的,不也是要解码的吗?
好了,你倒过来做不就知道什么样的数据开怎么样发送了吗?

你这个也是一个思路,问题又回到自己熟悉的php的层面,总比研究flex或者c#代码好。

你找份 amf 通讯协议的文档看看不就知道该怎么做了?

你找份 amf 通讯协议的文档看看不就知道该怎么做了?
本来毫无头绪,你这两句话提醒了我,谢谢

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn