PHP写的二元响应 [英] PHP write binary response

查看:197
本文介绍了PHP写的二元响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP是有写二进制数据响应流的方式,
结果
等的等效(C#ASP)
结果

  System.IO.BinaryWriter二进制=新System.IO.BinaryWriter(Response.OutputStream);
Binary.Write((System.Int32 1)); // 01000000
Binary.Write((System.Int32)1020); // FC030000
Binary.Close();

结果
结果
那么我想能够读取C#应用程序的响应,如

  System.Net.HttpWebRequest请求=(System.Net.HttpWebRequest)System.Net.WebRequest.Create(URI);
System.IO.BinaryReader二进制=新System.IO.BinaryReader(Request.GetResponse()GetResponseStream());
System.Int32 I = Binary.ReadInt32(); // 1
I = Binary.ReadInt32(); // 1020
Binary.Close();


解决方案

在PHP中,字符串和字节数组是同一个。使用以创建一个字节数组(串),您可以再写入。一旦我意识到,生活变得更容易。

  $ my_byte_array =包(LL,为0x01000000,0xFC030000);
$ FP =的fopen(somefile.txt,W);
FWRITE($ FP,$ my_byte_array);//或者只是呼应到stdout
回声$ my_byte_array;

In php is there a way to write binary data to the response stream,
like the equivalent of (c# asp)

System.IO.BinaryWriter Binary = new System.IO.BinaryWriter(Response.OutputStream);
Binary.Write((System.Int32)1);//01000000
Binary.Write((System.Int32)1020);//FC030000
Binary.Close();



I would then like to be able read the response in a c# application, like

System.Net.HttpWebRequest Request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("URI");
System.IO.BinaryReader Binary = new System.IO.BinaryReader(Request.GetResponse().GetResponseStream());
System.Int32 i = Binary.ReadInt32();//1
i = Binary.ReadInt32();//1020
Binary.Close();

解决方案

In PHP, strings and byte arrays are one and the same. Use pack to create a byte array (string) that you can then write. Once I realized that, life got easier.

$my_byte_array = pack("LL", 0x01000000, 0xFC030000);
$fp = fopen("somefile.txt", "w");
fwrite($fp, $my_byte_array);

// or just echo to stdout
echo $my_byte_array;

这篇关于PHP写的二元响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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