I am using PHP to perform a POST request from a socket. Here is a code snippet:
// open a socket connection on port 80
$fp = fsockopen($host, 80);
// send the request headers:
fwrite($fp, "POST $path HTTP/1.1\r\n");
fwrite($fp, "Host: $host\r\n");
fwrite($fp, "Referer: $referer\r\n");
fwrite($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-length: ". strlen($data) ."\r\n");
fwrite($fp, "Connection: close\r\n\r\n");
fwrite($fp, $data);
$result = '';
while(!feof($fp)) {
// receive the results of the request
$result .= fread($fp, 5000);
}
However, each line of the response seems to be prefixed with its length in hex. Why is that happening?
For example, a test cgi script, to which I post, returns its environment:
BASH=/bin/bash
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
But my PHP function ends up with this:
f
BASH=/bin/bash
69
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
10
BASH_ALIASES=()
You should use
HTTP/1.0, because 1.1 servers typically respond with the Transfer-Encoding:chunked(= which are the parts prefixed by hex numbers).You could try to send a request begging for
identitycontent encoding, but chunked is required from all HTTP/1.1 clients as transfer format, so that might be unreliabe.