J'utilise PHP pour effectuer une requête POST depuis un socket. Voici un extrait de code:Pourquoi la fonction php fread() préfixe-t-elle parfois chaque ligne avec sa longueur?
// 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);
}
Cependant, chaque ligne de la réponse semble être préfixé par sa longueur dans l'hexagone. Pourquoi cela se passe-t-il?
Par exemple, un script cgi test, auquel je poste, retourne son environnement:
BASH=/bin/bash
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
Mais ma fonction PHP se termine avec ceci:
f
BASH=/bin/bash
69
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
10
BASH_ALIASES=()