-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathexamples.xml
259 lines (224 loc) · 6.83 KB
/
examples.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: a3479b788cab353af804fe89d14ec45ba897efae Maintainer: takagi Status: ready -->
<!-- CREDITS: shimooka,hirokawa -->
<chapter xml:id="stream.examples">
&reftitle.examples;
<para>
<example>
<title><function>file_get_contents</function> を使って
さまざまな場所からデータを取得する</title>
<programlisting role="php">
<![CDATA[
<?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:[email protected]/foo.txt");
/* FTPS を利用し、ftp.example.com にあるリモートのファイルを読み出す */
$ftpsfile = file_get_contents("ftps://user:[email protected]/foo.txt");
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>https のサーバーに対して POST リクエストを行う</title>
<programlisting role="php">
<![CDATA[
<?php
/* https://secure.example.com/form_action.php に対して POST リクエストを送信
* ダミー値を持つ "foo" と "bar" というフォーム要素が含まれます。
*/
$sock = fsockopen("ssl://secure.example.com", 443, $errno, $errstr, 30);
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);
$headers = "";
while ($str = trim(fgets($sock, 4096)))
$headers .= "$str\n";
echo "\n";
$body = "";
while (!feof($sock))
$body .= fgets($sock, 4096);
fclose($sock);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>データを圧縮ファイルに書き込む</title>
<programlisting role="php">
<![CDATA[
<?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);
?>
]]>
</programlisting>
</example>
</para>
<section xml:id="stream.streamwrapper.example-1">
<title>ストリームラッパーとして登録するクラスの例</title>
<para>
以下のサンプルは、var:// プロトコルハンドラを実装し、
指定した名前のグローバル変数の読み書きを
<function>fread</function> のような標準のファイルシステム関数でできるようにしています。
以下で実装した var:// プロトコルでは、URL "var://foo" で
$GLOBALS["foo"] への読み書きを行います。
</para>
<para>
<example>
<title>グローバル変数の読み書き用のストリーム</title>
<programlisting role="php">
<![CDATA[
<?php
class VariableStream {
var $position;
var $varname;
function stream_open($path, $mode, $options, &$opened_path)
{
$url = parse_url($path);
$this->varname = $url["host"];
$this->position = 0;
return true;
}
function stream_read($count)
{
$ret = substr($GLOBALS[$this->varname], $this->position, $count);
$this->position += strlen($ret);
return $ret;
}
function stream_write($data)
{
$left = substr($GLOBALS[$this->varname], 0, $this->position);
$right = substr($GLOBALS[$this->varname], $this->position + strlen($data));
$GLOBALS[$this->varname] = $left . $data . $right;
$this->position += strlen($data);
return strlen($data);
}
function stream_tell()
{
return $this->position;
}
function stream_eof()
{
return $this->position >= strlen($GLOBALS[$this->varname]);
}
function stream_seek($offset, $whence)
{
switch ($whence) {
case SEEK_SET:
if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
$this->position = $offset;
return true;
} else {
return false;
}
break;
case SEEK_CUR:
if ($offset >= 0) {
$this->position += $offset;
return true;
} else {
return false;
}
break;
case SEEK_END:
if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
$this->position = strlen($GLOBALS[$this->varname]) + $offset;
return true;
} else {
return false;
}
break;
default:
return false;
}
}
function stream_metadata($path, $option, $var)
{
if($option == STREAM_META_TOUCH) {
$url = parse_url($path);
$varname = $url["host"];
if(!isset($GLOBALS[$varname])) {
$GLOBALS[$varname] = '';
}
return true;
}
return false;
}
}
stream_wrapper_register("var", "VariableStream")
or die("Failed to register protocol");
$myvar = "";
$fp = fopen("var://myvar", "r+");
fwrite($fp, "line1\n");
fwrite($fp, "line2\n");
fwrite($fp, "line3\n");
rewind($fp);
while (!feof($fp)) {
echo fgets($fp);
}
fclose($fp);
var_dump($myvar);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
line1
line2
line3
string(18) "line1
line2
line3
"
]]>
</screen>
</example>
</para>
</section>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->