Stream Errors
<<<
Exemples Index des fonctions
>>>

6.98 Stream functions
6 Index des fonctions
 Manuel PHP

Introduction
Pré-requis
Installation
Configuration à l'exécution
Stream Classes
Constantes Prédefinies
Stream Errors
->Exemples

6.98.8 Exemples

Using file_get_contents to retrieve data from multiple sources

<?php
$localfile
= file_get_contents("/home/bar/foo.txt");                         // Read local file from /home/bar
$localfile = file_get_contents("file:///home/bar/foo.txt");                  // Identical to above, explicitly naming FILE scheme
$httpfile  = file_get_contents("http://www.example.com/foo.txt");            // Read remote file from www.example.com using HTTP
$httpsfile = file_get_contents("https://www.example.com/foo.txt");           // Read remote file from www.example.com using HTTPS
$ftpfile   = file_get_contents("ftp://user:pass@ftp.example.com/foo.txt");   // Read remote file from ftp.example.com using FTP
$ftpsfile  = file_get_contents("ftps://user:pass@ftp.example.com/foo.txt");  // Read remote file from ftp.example.com using FTPS
?>

Making a POST request to an https server

<?php
/* Send POST request to https://secure.example.com/form_action.php
* Include form elements named "foo" and "bar" with dummy values
*/

$sock = fsockopen("ssl://secure.example.com", 443, $errno, $errstr, 30);
if (!
$sock) die("$errstr ($errno)\n";

$data = "foo=" . urlencode("Value for Foo") . "&bar=" . urlencode("Value for Bar");

fputs($sock, "POST /form_action.php HTTP/1.0\r\n");
fputs($sock, "Host: secure.example.com\r\n");
fputs($sock, "Content-type: application/x-www-url-encoded\r\n");
fputs($sock, "Content-length: " . strlen($data) . "\r\n");
fputs($sock, "Accept: */*\r\n");
fputs($sock, "\r\n");
fputs($sock, "$data\r\n");
fputs($sock, "\r\n");

$headers = "";
while (
$str = trim(fgets($sock, 4096)))
  
$headers .= "$str\n";

print
"\n";

$body = "";
while (!
feof($sock))
  
$body .= fgets($sock, 4096);

fclose($sock);
?>

Writting data to a compressed file

<?php
/* Create a compressed file containing an arbitrarty string
* File can be read back using compress.zlib stream or just
* decompressed from the command line using 'gzip -d foo-bar.txt.gz'
*/
$fp = fopen("compress.zlib://foo-bar.txt.gz","w");
if (!
$fp) die("Unable to create file.");

fwrite($fp, "This is a test.\n");

fclose($fp);
?>

<< Exemples >>
Stream Errors Stream functions Index des fonctions