J'ai un comportement étrange sur un serveur ubuntu (nginx pile php5-FPM) code suivant explique le problème:file_get_contents url renvoie la chaîne vide, alors que fsockopen fonctionne bien?
<?php
// WORKS:
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET/HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
// DOES NOT WORK! Return empty string (no error like NULL or False whatsoever).
echo file_get_contents("http://www.example.com");
// allow_ftp_open is On
echo var_dump(ini_get('allow_url_fopen')); // returns '1'
?>
code suivant fopen:
<?php
// get contents of a file into a string
$filename = "http://www.example.com";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
?>
Génère ceci:
Warning: filesize(): stat failed for http://www.example.com in test.php on line 5
Warning: fread(): Length parameter must be greater than 0 in test.php on line 5
Si j'utilise une taille fixe dans fread, le résultat est une chaîne vide comme avec file_get_contents. Comment peut-il être que les douilles fonctionnent bien, mais ne fonctionne pas? Qu'est-ce que je rate?
À quoi sert allow_url_fopen? – bksi
Activé. var_dump (ini_get ('allow_url_fopen')) renvoie "1" –