例1 file_get_contents() を使って さまざまな場所からデータを取得する

<?php
/* /home/bar にあるローカルファイルを読み出す */
$localfile file_get_contents("/home/bar/foo.txt");

/* 上と同一だが、明示的に FILE スキームを指定している */
$localfile file_get_contents("file:///home/bar/foo.txt");

/* HTTP を利用し、www.example.com にあるリモートのファイルを読み出す */
$httpfile  file_get_contents("http://www.example.com/foo.txt");

/* HTTPS を利用し、www.example.com にあるリモートのファイルを読み出す */
$httpsfile file_get_contents("https://www.example.com/foo.txt");

/* FTP を利用し、ftp.example.com にあるリモートのファイルを読み出す */
$ftpfile   file_get_contents("ftp://user:pass@ftp.example.com/foo.txt");

/* FTPS を利用し、ftp.example.com にあるリモートのファイルを読み出す */
$ftpsfile  file_get_contents("ftps://user:pass@ftp.example.com/foo.txt");
?>

例2 https のサーバに対して POST リクエストを行う

<?php
/* https://secure.example.com/form_action.php に対して POST リクエストを送信
 * ダミー値を持つ "foo" と "bar" というフォーム要素が含まれます。
*/

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

$data "foo=" urlencode("Fooの値") . "&bar=" urlencode("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);
?>

例3 データを圧縮ファイルに書き込む

<?php
/* 任意の文字列を含む圧縮ファイルを作成
 * ファイルは、compress.zlib ストリームを使っても、または単に
 * コマンドラインで 'gzip -d foo-bar.txt.gz' を使っても読み出せます。
*/
$fp fopen("compress.zlib://foo-bar.txt.gz""wb");
if (!
$fp) die("ファイルが作成できません。");

fwrite($fp"これはテストです。\n");

fclose($fp);
?>


NPO法人の設立ならNPO-office

NPO法人を設立専門事務所。特定非営利活動法人の略であるNPO法人を内閣府をはじめ全国都道府県対応で支援。

アルバイトをお探しならアルバイトスタイルへ。アルバイトを探したい方、アルバイトを探している方は必見です。
NPO法人をこれから設立したいとお考えなら自分で設立するNPO法人へ。都道府県ごとのNPO法人設立情報などを提供。NPO法人証明書も発行
株式会社をこれから設立したいとお考えなら会社設立webへ。都道府県ごとの会社設立専門家を紹介しております。
看護師・看護婦

看護師・看護婦のための転職・紹介。しばらく看護師として離れていたかたも

人材派遣・アルバイト紹介

人材派遣やアルバイト情報を集めました。