Örnekler

İçindekiler

Örnek 1 - Çok sayıda kaynaktan veri almak için file_get_contents() kullanımı

<?php
/* Yerel dosyayı /home/bar dizininden okuyalım */
$localfile file_get_contents("/home/bar/foo.txt");

/* FILE sarmalayıcı kullanımı dışında yukarıdakine eşdeğerdir */
$localfile file_get_contents("file:///home/bar/foo.txt");

/* Uzak dosyayı HTTP kullaranak www.example.com'dan okur */
$httpfile  file_get_contents("http://www.example.com/foo.txt");

/* Uzak dosyayı HTTPS kullaranak www.example.com'dan okur */
$httpsfile file_get_contents("https://www.example.com/foo.txt");

/* Uzak dosyayı FTP kullaranak ftp.example.com'dan okur */
$ftpfile   file_get_contents("ftp://user:[email protected]/foo.txt");

/* Uzak dosyayı FTPS kullaranak ftp.example.com'dan okur */
$ftpsfile  file_get_contents("ftps://user:[email protected]/foo.txt");
?>

Örnek 2 - Bir HTTPS sunucusundan bir POST isteği yapmak

<?php
/* Post isteğini https://secure.example.com/form_action.php adresine gönderelim
 * Dosyada "foo" and "bar" isimli form elemanları olsun.
 */

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

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

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

$headers "";
while (
$str trim(fgets($sock4096)))
$headers .= "$str\n";

echo 
"\n";

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

fclose($sock);
?>

Örnek 3 - Sıkıştırılmış bir dosyaya veri yazmak

<?php
/* Bir dizge içeren sıkıştırılmış bir dosya oluşturalım.
 * Dosya, compress.zlib akımı kullanılarak veya
 * komut satırından 'gzip -d foo-bar.txt.gz' kullanılarak
 * kodlaması açılıp okunabilir.
 */
$fp fopen("compress.zlib://foo-bar.txt.gz""wb");
if (!
$fp) die("Dosya oluşturulamadı.");

fwrite($fp"Bu bir denemedir.\n");

fclose($fp);
?>