PHP - 用二进制数据打包数组

有一个服务(php)将图像发送到客户端(php)。


header('Content-Type: image/png');

readfile($image);

如果不仅需要发送图像,还需要发送一些数据,该怎么办。


$arrayToSend = [

    'image' => file_get_contents($image),

    'some_data' => [

        'a' => 1,

        'b' => 2

    ]

];

服务如何打包 $arrayToSend 以便客户端可以解包?


不将图像转换为 base64(因为尺寸太大)。


慕姐8265434
浏览 133回答 2
2回答

慕森卡

如果您通过 HTTP 通信,@Danon 的标头方法可能是要走的路,但其他传输可能不支持发送额外的标头,因此您需要将它们与二进制数据一起打包,然后在接收端解包。<?phpclass Codec{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Pack metadata along with binary data&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param $meta&nbsp; &nbsp; &nbsp;* @param $data&nbsp; &nbsp; &nbsp;* @return false|string&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static function encode($meta, $data)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $meta = base64_encode($meta);&nbsp; &nbsp; &nbsp; &nbsp; //determine length of metadata&nbsp; &nbsp; &nbsp; &nbsp; $metaLength = strlen($meta);&nbsp; &nbsp; &nbsp; &nbsp; //The first part of the message is the metadata length&nbsp; &nbsp; &nbsp; &nbsp; $output = pack('VA*', $metaLength, $meta);&nbsp; &nbsp; &nbsp; &nbsp; //Length and metadata are set, now include the binary data&nbsp; &nbsp; &nbsp; &nbsp; $output .= $data;&nbsp; &nbsp; &nbsp; &nbsp; return $output;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Unpack data encoded via the encode function.&nbsp; &nbsp; &nbsp;* Returns an array with "meta" and "data" elaments&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param $content&nbsp; &nbsp; &nbsp;* @return array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static function decode($content)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //Get the length of the metadata content&nbsp; &nbsp; &nbsp; &nbsp; $metaLength = unpack('V', $content)[1];&nbsp; &nbsp; &nbsp; &nbsp; //Slice out the metatdata, offset 4 to account for the length bytes&nbsp; &nbsp; &nbsp; &nbsp; $metaPacked = substr($content, 4, $metaLength);&nbsp; &nbsp; &nbsp; &nbsp; //Unpack and base64 decode the metadata&nbsp; &nbsp; &nbsp; &nbsp; $meta = unpack('A*', $metaPacked)[1];&nbsp; &nbsp; &nbsp; &nbsp; $meta = base64_decode($meta);&nbsp; &nbsp; &nbsp; &nbsp; //The binary data is everything after the metadata&nbsp; &nbsp; &nbsp; &nbsp; $data = substr($content, $metaLength+4);&nbsp; &nbsp; &nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'meta' => $meta,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'data' => $data&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; }}//Load contents of a binary file$imageFilePath = 'path_to_image.png';$data = file_get_contents($imageFilePath);//Arbitrary metadata - could be anything, let's use JSON$meta = [&nbsp; &nbsp; 'filename' => 'foo.png',&nbsp; &nbsp; 'uid' => 12345,&nbsp; &nbsp; 'md5' => md5_file($imageFilePath)];$metaJson = json_encode($meta);//Encode the message, you can then send this to the receiver$payload = Codec::encode($metaJson, $data);//Receiver decodes the message$result = Codec::decode($payload);//Decode our JSON string$resultMeta = json_decode($result['meta'], true);echo 'Filename: '.$resultMeta['filename'].PHP_EOL;echo 'UID: '.$resultMeta['uid'].PHP_EOL;//We included an MD5 hash of the file, so we can verify hereif($resultMeta['md5'] != md5($result['data'])){&nbsp; &nbsp; echo 'MD5 mismatch!';}
打开App,查看更多内容
随时随地看视频慕课网APP