d71fda90b43b81103cc7a2cb23704c901b546fd0
[mdref/mdref-http] / http / Message / Body / addForm.md
1 # http\Message\Body http\Message\Body::addForm([array $fields = NULL[, array $files = NULL]])
2
3 Add form fields and files to the message body.
4
5 > **Note:** Currently, http\Message\Body::addForm() creates "multipart/form-data" bodies.
6
7 ## Params:
8
9 * array $fields = NULL
10 List of form fields to add.
11
12 * array $files = NULL
13 List of form files to add.
14
15 $fields must look like:
16
17 [
18 "field_name" => "value",
19 "multi_field" => [
20 "value1",
21 "value2"
22 ]
23 ]
24
25 $files must look like:
26
27 [
28 [
29 "name" => "field_name",
30 "type" => "content/type",
31 "file" => "/path/to/file.ext"
32 ], [
33 "name" => "field_name2",
34 "type" => "text/plain",
35 "file" => "file.ext",
36 "data" => "string"
37 ], [
38 "name" => "field_name3",
39 "type" => "image/jpeg",
40 "file" => "file.ext",
41 "data" => fopen("/home/mike/Pictures/mike.jpg","r")
42 ]
43
44 As you can see, a file structure must contain a "file" entry, which holds a file path, and an optional "data" entry, which may either contain a resource to read from or the actual data as string.
45
46 ## Returns:
47
48 * http\Message\Body, self.
49
50 ## Example:
51
52 <?php
53 $body = new http\Message\Body;
54 $body->addForm([
55 "field_name" => "value",
56 "multi_field" => [
57 "value1",
58 "value2"
59 ]
60 ], [
61 [
62 "name" => "field_name",
63 "type" => "application/octet-stream",
64 "file" => "/run/gpm.pid"
65 ], [
66 "name" => "field_name2",
67 "type" => "text/plain",
68 "file" => "signature.txt",
69 "data" => "-- \nMike\n"
70 ], [
71 "name" => "field_name3",
72 "type" => "image/jpeg",
73 "file" => "picture.jpg",
74 "data" => fopen("/home/mike/Pictures/mike.jpg","r")
75 ]
76 ]);
77
78 echo $body;
79 ?>
80
81 Yields:
82
83 --32260b4b.3fea9114
84 Content-Disposition: form-data; name="field_name"
85
86 value
87 --32260b4b.3fea9114
88 Content-Disposition: form-data; name="multi_field[0]"
89
90 value1
91 --32260b4b.3fea9114
92 Content-Disposition: form-data; name="multi_field[1]"
93
94 value2
95 --32260b4b.3fea9114
96 Content-Disposition: form-data; name="field_name"; filename="gpm.pid"
97 Content-Transfer-Encoding: binary
98 Content-Type: application/octet-stream
99
100 316
101
102 --32260b4b.3fea9114
103 Content-Disposition: form-data; name="field_name2"; filename="signature.txt"
104 Content-Transfer-Encoding: binary
105 Content-Type: text/plain
106
107 --
108 Mike
109
110 --32260b4b.3fea9114
111 Content-Disposition: form-data; name="field_name3"; filename="picture.jpg"
112 Content-Transfer-Encoding: binary
113 Content-Type: image/jpeg
114
115 ...JPEG...
116 --32260b4b.3fea9114--