Skip to content

Commit f56de7e

Browse files
authored
Improve $_POST/$_GET definition (#4522)
* Improve $_POST/$_GET definition * end example sentences with a dot * $_GET: apply suggestions * Do not use title case in refpurpose * docs(post): reference php://input * docs(php://input): provide example to read JSON data * fix format in previous commits * docs: apply suggestions * docs: remove try/catch and improve variable names in example
1 parent 873f4a3 commit f56de7e

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

language/predefined/variables/get.xml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
<refentry role="variable" xml:id="reserved.variables.get" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
55
<refnamediv>
66
<refname>$_GET</refname>
7-
<refpurpose>HTTP GET variables</refpurpose>
7+
<refpurpose>Query string variables</refpurpose>
88
</refnamediv>
99

1010
<refsect1 role="description">
1111
&reftitle.description;
1212
<para>
13-
An associative array of variables passed to the current script
14-
via the URL parameters (aka. query string). Note that the array is not only
15-
populated for GET requests, but rather for all requests with a query string.
13+
An associative array of variables passed to the current script via the URL
14+
parameters (also known as the query string). Note that this array is
15+
populated whenever a query string is present, regardless of the HTTP request
16+
method.
1617
</para>
1718
</refsect1>
1819

@@ -29,7 +30,7 @@ echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
2930
]]>
3031
</programlisting>
3132
<simpara>
32-
Assuming the user entered http://example.com/?name=Hannes
33+
Assuming the user entered <literal>http://example.com/?name=Hannes</literal>.
3334
</simpara>
3435
&example.outputs.similar;
3536
<screen>
@@ -46,7 +47,7 @@ Hello Hannes!
4647
&note.is-superglobal;
4748
<note>
4849
<para>
49-
The GET variables are passed through <function>urldecode</function>.
50+
The values in <varname>$_GET</varname> are automatically passed through <function>urldecode</function>.
5051
</para>
5152
</note>
5253
</refsect1>

language/predefined/variables/post.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<refentry role="variable" xml:id="reserved.variables.post" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
55
<refnamediv>
66
<refname>$_POST</refname>
7-
<refpurpose>HTTP POST variables</refpurpose>
7+
<refpurpose>Form data from HTTP POST requests</refpurpose>
88
</refnamediv>
99

1010
<refsect1 role="description">
@@ -29,7 +29,8 @@ echo 'Hello ' . htmlspecialchars($_POST["name"]) . '!';
2929
]]>
3030
</programlisting>
3131
<simpara>
32-
Assuming the user POSTed name=Hannes
32+
Assuming the user sent a POST request with <literal>name=Hannes</literal>
33+
in the body.
3334
</simpara>
3435
&example.outputs.similar;
3536
<screen>
@@ -44,6 +45,17 @@ Hello Hannes!
4445
<refsect1 role="notes">
4546
&reftitle.notes;
4647
&note.is-superglobal;
48+
<note>
49+
<simpara>
50+
To read POST data sent with other content types (e.g.
51+
<literal>application/json</literal> or <literal>application/xml</literal>)
52+
<link linkend="wrappers.php.input"><filename>php://input</filename></link>
53+
must be used. Unlike <varname>$_POST</varname>, which only works with
54+
<literal>application/x-www-form-urlencoded</literal> and
55+
<literal>multipart/form-data</literal>, <filename>php://input</filename>
56+
provides direct access to the raw data from the body of the request.
57+
</simpara>
58+
</note>
4759
</refsect1>
4860

4961
<refsect1 role="seealso">

language/wrappers/php.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,28 @@ file_put_contents("php://filter/write=string.rot13/resource=example.txt","Hello
368368
<?php
369369
file_put_contents('php://memory', 'PHP');
370370
echo file_get_contents('php://memory'); // prints nothing
371+
]]>
372+
</programlisting>
373+
</example>
374+
<example>
375+
<title>php://input to read JSON data from the request body</title>
376+
<para>
377+
This example demonstrates how to read raw JSON data from POST, PUT and
378+
PATCH requests using <filename>php://input</filename>.
379+
</para>
380+
<programlisting role="php">
381+
<![CDATA[
382+
<?php
383+
$input = file_get_contents("php://input");
384+
$json_array = json_decode(
385+
json: $input,
386+
associative: true,
387+
flags: JSON_THROW_ON_ERROR
388+
);
389+
390+
echo "Received JSON data: ";
391+
print_r($json_array);
392+
?>
371393
]]>
372394
</programlisting>
373395
</example>

0 commit comments

Comments
 (0)