Examples

Table of Contents

Example #1 Using file_get_contents() to retrieve data from multiple sources

<?php

$localfile = file_get_contents("/home/bar/foo.txt");


$localfile = file_get_contents("file:///home/bar/foo.txt");


$httpfile = file_get_contents("http://www.example.com/foo.txt");


$httpsfile = file_get_contents("https://www.example.com/foo.txt");


$ftpfile = file_get_contents("ftp://user:pass@ftp.example.com/foo.txt");


$ftpsfile = file_get_contents("ftps://user:pass@ftp.example.com/foo.txt");
?>

Example #2 Making a POST request to an https server

<?php


$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");

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: *
$fp = fopen("compress.zlib://foo-bar.txt.gz", "wb");
if (!
$fp) die("Unable to create file.");

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

fclose($fp);
?>
To Top