문제

I want to send a custom HTTP response back to an application requesting a GET to a php script. The body is will be in binary format (octet-streams). I'm wondering if there is a simple way to do this? I am looking into the HttpResponse class in PECL but is having trouble installing it right now. I do not really need all the other functionalities that goes with it so I'm looking for something simpler.

Any help would be appreciated.

도움이 되었습니까?

해결책

PHP has a header() function built in, so you can customise your response without requiring extra libraries:

<?php

header('Content-Type: application/octet-stream');
header('X-Powered-By: l3utterfly');

echo $binary_data;

?>

다른 팁

You can always set HTTP Headers using header() function, and then simply output binary data using print, echo or any other usual way. Send Content-Type http header to octet stream and it should work all right.

You can use the header function to send back whatever response you want. If you want to send back custome response codes, you could use:

<?
  $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');

  //change the code and message to whatever. But I would keep it valid codes so browsers can handle it.
  //see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

  header($protocol . ' 404 Not Found');
  exit();
?>

And if you want to send binary data, change the header to the correct content-type and echo the binary data.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top