xref: /web-php/manual/en/context.http.php (revision c3f96532)
1<?php
2include_once __DIR__ . '/../../include/shared-manual.inc';
3$TOC = array();
4$PARENTS = array();
5include_once __DIR__ ."/toc/context.inc";
6$setup = array (
7  'home' =>
8  array (
9    0 => 'index.php',
10    1 => 'PHP Manual',
11  ),
12  'head' =>
13  array (
14    0 => 'UTF-8',
15    1 => 'en',
16  ),
17  'this' =>
18  array (
19    0 => 'context.http.php',
20    1 => 'HTTP context options',
21  ),
22  'up' =>
23  array (
24    0 => 'context.php',
25    1 => 'Context options and parameters',
26  ),
27  'prev' =>
28  array (
29    0 => 'context.socket.php',
30    1 => 'Socket context options',
31  ),
32  'next' =>
33  array (
34    0 => 'context.ftp.php',
35    1 => 'FTP context options',
36  ),
37  'alternatives' =>
38  array (
39  ),
40);
41$setup["toc"] = $TOC;
42$setup["parents"] = $PARENTS;
43manual_setup($setup);
44
45?>
46<div id="context.http" class="refentry">
47 <div class="refnamediv">
48  <h1 class="refname">HTTP context options</h1>
49  <p class="refpurpose"><span class="refname">HTTP context options</span> &mdash; <span class="dc-title">HTTP context option listing</span></p>
50
51 </div>
52
53 <div class="refsect1 description" id="refsect1-context.http-description">
54  <h3 class="title">Description</h3>
55  <p class="para">
56   Context options for <em>http://</em> and <em>https://</em>
57   transports.
58  </p>
59 </div>
60
61
62 <div class="refsect1 options" id="refsect1-context.http-options">
63  <h3 class="title">Options</h3>
64  <p class="para">
65   <dl>
66
67    <dt id="context.http.method">
68     <span class="term">
69      <em><code class="parameter">method</code></em>
70      <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
71     </span>
72     <dd>
73
74      <p class="para">
75       <strong><code>GET</code></strong>, <strong><code>POST</code></strong>, or
76       any other HTTP method supported by the remote server.
77      </p>
78      <p class="para">
79       Defaults to <strong><code>GET</code></strong>.
80      </p>
81     </dd>
82
83    </dt>
84
85    <dt id="context.http.header">
86     <span class="term">
87      <em><code class="parameter">header</code></em>
88      <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
89     </span>
90     <dd>
91
92      <p class="para">
93       Additional headers to be sent during request. Values
94       in this option will override other values (such as
95       <em>User-agent:</em>, <em>Host:</em>,
96       and <em>Authentication:</em>).
97      </p>
98     </dd>
99
100    </dt>
101
102    <dt id="context.http.user-agent">
103     <span class="term">
104      <em><code class="parameter">user_agent</code></em>
105      <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
106     </span>
107     <dd>
108
109      <p class="para">
110       Value to send with <em>User-Agent:</em> header. This value will
111       only be used if user-agent is <em class="emphasis">not</em> specified
112       in the <em>header</em> context option above.
113      </p>
114      <p class="para">
115       By default the
116       <a href="filesystem.configuration.php#ini.user-agent" class="link">user_agent</a>
117       <var class="filename">php.ini</var> setting is used.
118      </p>
119     </dd>
120
121    </dt>
122
123    <dt id="context.http.content">
124     <span class="term">
125      <em><code class="parameter">content</code></em>
126      <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
127     </span>
128     <dd>
129
130      <p class="para">
131       Additional data to be sent after the headers. Typically used
132       with POST or PUT requests.
133      </p>
134     </dd>
135
136    </dt>
137
138    <dt id="context.http.proxy">
139     <span class="term">
140      <em><code class="parameter">proxy</code></em>
141      <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
142     </span>
143     <dd>
144
145      <p class="para">
146       URI specifying address of proxy server. (e.g.
147       <em>tcp://proxy.example.com:5100</em>).
148      </p>
149     </dd>
150
151    </dt>
152
153    <dt id="context.http.request-fulluri">
154     <span class="term">
155      <em><code class="parameter">request_fulluri</code></em>
156      <span class="type"><a href="language.types.boolean.php" class="type boolean">boolean</a></span>
157     </span>
158     <dd>
159
160      <p class="para">
161       When set to <strong><code>TRUE</code></strong>, the entire URI will be used when
162       constructing the request.  (i.e.
163       <em>GET http://www.example.com/path/to/file.html HTTP/1.0</em>).
164       While this is a non-standard request format, some
165       proxy servers require it.
166      </p>
167      <p class="para">
168       Defaults to <strong><code>FALSE</code></strong>.
169      </p>
170     </dd>
171
172    </dt>
173
174    <dt id="context.http.follow-location">
175     <span class="term">
176      <em><code class="parameter">follow_location</code></em>
177      <span class="type"><a href="language.types.integer.php" class="type integer">integer</a></span>
178     </span>
179     <dd>
180
181      <p class="para">
182       Follow <em>Location</em> header redirects. Set to
183       <em>0</em> to disable.
184      </p>
185      <p class="para">
186       Defaults to <em>1</em>.
187      </p>
188     </dd>
189
190    </dt>
191
192    <dt id="context.http.max-redirects">
193     <span class="term">
194      <em><code class="parameter">max_redirects</code></em>
195      <span class="type"><a href="language.types.integer.php" class="type integer">integer</a></span>
196     </span>
197     <dd>
198
199      <p class="para">
200       The max number of redirects to follow. Value <em>1</em> or
201       less means that no redirects are followed.
202      </p>
203      <p class="para">
204       Defaults to <em>20</em>.
205      </p>
206     </dd>
207
208    </dt>
209
210    <dt id="context.http.protocol-version">
211     <span class="term">
212      <em><code class="parameter">protocol_version</code></em>
213      <span class="type"><a href="language.types.float.php" class="type float">float</a></span>
214     </span>
215     <dd>
216
217      <p class="para">
218       HTTP protocol version.
219      </p>
220      <p class="para">
221       Defaults to <em>1.0</em>.
222      </p>
223      <blockquote class="note"><p><strong class="note">Note</strong>:
224       <p class="para">
225        PHP prior to 5.3.0 does not implement chunked transfer decoding.
226        If this value is set to <em>1.1</em> it is your
227        responsibility to be <em>1.1</em> compliant.
228       </p>
229      </p></blockquote>
230     </dd>
231
232    </dt>
233
234    <dt id="context.http.timeout">
235     <span class="term">
236      <em><code class="parameter">timeout</code></em>
237      <span class="type"><a href="language.types.float.php" class="type float">float</a></span>
238     </span>
239     <dd>
240
241      <p class="para">
242       Read timeout in seconds, specified by a <span class="type"><a href="language.types.float.php" class="type float">float</a></span>
243       (e.g. <em>10.5</em>).
244      </p>
245      <p class="para">
246       By default the
247       <a href="filesystem.configuration.php#ini.default-socket-timeout" class="link">default_socket_timeout</a>
248       <var class="filename">php.ini</var> setting is used.
249      </p>
250     </dd>
251
252    </dt>
253
254    <dt id="context.http.ignore-errors">
255     <span class="term">
256      <em><code class="parameter">ignore_errors</code></em>
257      <span class="type"><a href="language.types.boolean.php" class="type boolean">boolean</a></span>
258     </span>
259     <dd>
260
261      <p class="para">
262       Fetch the content even on failure status codes.
263      </p>
264      <p class="para">
265       Defaults to <strong><code>FALSE</code></strong>.
266      </p>
267     </dd>
268
269    </dt>
270
271   </dl>
272
273  </p>
274 </div>
275
276
277 <div class="refsect1 changelog" id="refsect1-context.http-changelog">
278  <h3 class="title">Changelog</h3>
279  <p class="para">
280   <table class="doctable informaltable">
281
282     <thead>
283      <tr>
284       <th>Version</th>
285       <th>Description</th>
286      </tr>
287
288     </thead>
289
290     <tbody class="tbody">
291      <tr>
292       <td>5.3.4</td>
293       <td>
294        Added <em><code class="parameter">follow_location</code></em>.
295       </td>
296      </tr>
297
298      <tr>
299       <td>5.3.0</td>
300       <td>
301        The <em><code class="parameter">protocol_version</code></em> supports chunked transfer
302        decoding when set to <em>1.1</em>.
303       </td>
304      </tr>
305
306      <tr>
307       <td>5.2.10</td>
308       <td>
309        Added <em><code class="parameter">ignore_errors</code></em>.
310       </td>
311      </tr>
312
313      <tr>
314       <td>5.2.10</td>
315       <td>
316        The <em><code class="parameter">header</code></em> can now be an numerically indexed <span class="type"><a href="language.types.array.php" class="type array">array</a></span>.
317       </td>
318      </tr>
319
320      <tr>
321       <td>5.2.1</td>
322       <td>
323        Added <em><code class="parameter">timeout</code></em>.
324       </td>
325      </tr>
326
327      <tr>
328       <td>5.1.0</td>
329       <td>
330        Added HTTPS proxying through HTTP proxies.
331       </td>
332      </tr>
333
334      <tr>
335       <td>5.1.0</td>
336       <td>
337        Added <em><code class="parameter">max_redirects</code></em>.
338       </td>
339      </tr>
340
341      <tr>
342       <td>5.1.0</td>
343       <td>
344        Added <em><code class="parameter">protocol_version</code></em>.
345       </td>
346      </tr>
347
348     </tbody>
349
350   </table>
351
352  </p>
353 </div>
354
355
356 <div class="refsect1 examples" id="refsect1-context.http-examples">
357  <h3 class="title">Examples</h3>
358  <p class="para">
359   <div class="example" id="context.http.example-post">
360    <p><strong>Example #1 Fetch a page and send POST data</strong></p>
361    <div class="example-contents">
362<div class="phpcode"><code><span style="color: #000000">
363<span style="color: #0000BB">&lt;?php<br><br>$postdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">http_build_query</span><span style="color: #007700">(<br>&nbsp;&nbsp;&nbsp;&nbsp;array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'var1'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'some&nbsp;content'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'var2'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'doh'<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br>);<br><br></span><span style="color: #0000BB">$opts&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'http'&nbsp;</span><span style="color: #007700">=&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'method'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'POST'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'header'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Content-type:&nbsp;application/x-www-form-urlencoded'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'content'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$postdata<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br>);<br><br></span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br><br></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://example.com/submit.php'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br><br></span><span style="color: #0000BB">?&gt;</span>
364</span>
365</code></div>
366    </div>
367
368   </div>
369  </p>
370  <p class="para">
371   <div class="example" id="context.http.example-fetch-ignore-redirect">
372    <p><strong>Example #2 Ignore redirects but fetch headers and content </strong></p>
373    <div class="example-contents">
374<div class="phpcode"><code><span style="color: #000000">
375<span style="color: #0000BB">&lt;?php<br><br>$url&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"http://www.example.org/header.php"</span><span style="color: #007700">;<br><br></span><span style="color: #0000BB">$opts&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'http'&nbsp;</span><span style="color: #007700">=&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;array(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'method'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'GET'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'max_redirects'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'0'</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'ignore_errors'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'1'<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br>);<br><br></span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br></span><span style="color: #0000BB">$stream&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$url</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br><br></span><span style="color: #FF8000">//&nbsp;header&nbsp;information&nbsp;as&nbsp;well&nbsp;as&nbsp;meta&nbsp;data<br>//&nbsp;about&nbsp;the&nbsp;stream<br></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">stream_get_meta_data</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">));<br><br></span><span style="color: #FF8000">//&nbsp;actual&nbsp;data&nbsp;at&nbsp;$url<br></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">));<br></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br></span><span style="color: #0000BB">?&gt;</span>
376</span>
377</code></div>
378    </div>
379
380   </div>
381  </p>
382 </div>
383
384
385 <div class="refsect1 notes" id="refsect1-context.http-notes">
386  <h3 class="title">Notes</h3>
387  <blockquote class="note"><p><strong class="note">Note</strong>:
388   <strong>Underlying socket stream context options</strong><br>
389   <span class="simpara">
390    Additional context options may be supported by the
391    <a href="transports.inet.php" class="link">underlying transport</a>
392    For <em>http://</em> streams, refer to context
393    options for the <em>tcp://</em> transport.  For
394    <em>https://</em> streams, refer to context options
395    for the <em>ssl://</em> transport.
396   </span>
397  </p></blockquote>
398  <blockquote class="note"><p><strong class="note">Note</strong>:
399   <strong>HTTP status line</strong><br>
400   <span class="simpara">
401    When this stream wrapper follows a redirect, the
402    <em>wrapper_data</em> returned by
403     <span class="function"><a href="function.stream-get-meta-data.php" class="function">stream_get_meta_data()</a></span> might not necessarily contain
404    the HTTP status line that actually applies to the content data at index
405    <em>0</em>.
406   </span>
407   <div class="example-contents screen">
408<div class="cdata"><pre>
409array (
410  &#039;wrapper_data&#039; =&gt;
411  array (
412    0 =&gt; &#039;HTTP/1.0 301 Moved Permantenly&#039;,
413    1 =&gt; &#039;Cache-Control: no-cache&#039;,
414    2 =&gt; &#039;Connection: close&#039;,
415    3 =&gt; &#039;Location: http://example.com/foo.jpg&#039;,
416    4 =&gt; &#039;HTTP/1.1 200 OK&#039;,
417    ...
418</pre></div>
419   </div>
420   <span class="simpara">
421    The first request returned a <em>301</em> (permanent redirect),
422    so the stream wrapper automatically followed the redirect to get a
423    <em>200</em> response (index = <em>4</em>).
424   </span>
425  </p></blockquote>
426 </div>
427
428
429 <div class="refsect1 seealso" id="refsect1-context.http-seealso">
430  <h3 class="title">See Also</h3>
431  <p class="para">
432   <ul class="simplelist">
433    <li class="member"><a href="wrappers.http.php" class="xref">http://</a></li>
434    <li class="member"><a href="context.socket.php" class="xref">Socket context options</a></li>
435    <li class="member"><a href="context.ssl.php" class="xref">SSL context options</a></li>
436   </ul>
437  </p>
438 </div>
439
440
441</div><?php manual_footer(); ?>
442