0

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.

l3utterfly
  • 2,106
  • 4
  • 32
  • 58

3 Answers3

2

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.

Mołot
  • 699
  • 12
  • 36
2

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;

?>
Emily Shepherd
  • 1,369
  • 9
  • 20
  • Oh... I never knew you can echo binary data... (I'm new to PHP :P) Thanks for the answer, this is much simpler than expected! – l3utterfly Jun 17 '13 at 14:05
  • Follow up question: Can I use header to output custom headers? For example header("custom-header: 121316818"); My application will know how to parse this so I'm not really concerned with conforming to HTTP standards. – l3utterfly Jun 17 '13 at 14:08
1

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.

Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72