Cheetah
RequestBodyMultipart.php
Go to the documentation of this file.
1 <?php
24 namespace Facebook\Http;
25 
27 
38 {
42  private $boundary;
43 
47  private $params;
48 
52  private $files = [];
53 
59  public function __construct(array $params = [], array $files = [], $boundary = null)
60  {
61  $this->params = $params;
62  $this->files = $files;
63  $this->boundary = $boundary ?: uniqid();
64  }
65 
69  public function getBody()
70  {
71  $body = '';
72 
73  // Compile normal params
74  $params = $this->getNestedParams($this->params);
75  foreach ($params as $k => $v) {
76  $body .= $this->getParamString($k, $v);
77  }
78 
79  // Compile files
80  foreach ($this->files as $k => $v) {
81  $body .= $this->getFileString($k, $v);
82  }
83 
84  // Peace out
85  $body .= "--{$this->boundary}--\r\n";
86 
87  return $body;
88  }
89 
95  public function getBoundary()
96  {
97  return $this->boundary;
98  }
99 
108  private function getFileString($name, FacebookFile $file)
109  {
110  return sprintf(
111  "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"%s\r\n\r\n%s\r\n",
112  $this->boundary,
113  $name,
114  $file->getFileName(),
115  $this->getFileHeaders($file),
116  $file->getContents()
117  );
118  }
119 
128  private function getParamString($name, $value)
129  {
130  return sprintf(
131  "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n",
132  $this->boundary,
133  $name,
134  $value
135  );
136  }
137 
145  private function getNestedParams(array $params)
146  {
147  $query = http_build_query($params, null, '&');
148  $params = explode('&', $query);
149  $result = [];
150 
151  foreach ($params as $param) {
152  list($key, $value) = explode('=', $param, 2);
153  $result[urldecode($key)] = urldecode($value);
154  }
155 
156  return $result;
157  }
158 
166  protected function getFileHeaders(FacebookFile $file)
167  {
168  return "\r\nContent-Type: {$file->getMimetype()}";
169  }
170 }
Facebook\Http\RequestBodyMultipart\getBoundary
getBoundary()
Definition: RequestBodyMultipart.php:95
Facebook\Http\RequestBodyMultipart\getBody
getBody()
Definition: RequestBodyMultipart.php:69
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
php
Facebook\Http\RequestBodyInterface
Definition: RequestBodyInterface.php:32
Facebook\Http
Definition: GraphRawResponse.php:24
Facebook\FileUpload\FacebookFile\getFileName
getFileName()
Definition: FacebookFile.php:123
Facebook\Http\RequestBodyMultipart
Definition: RequestBodyMultipart.php:38
Facebook\FileUpload\FacebookFile\getContents
getContents()
Definition: FacebookFile.php:113
Facebook\Http\RequestBodyMultipart\getFileHeaders
getFileHeaders(FacebookFile $file)
Definition: RequestBodyMultipart.php:166
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
Facebook\Http\RequestBodyMultipart\__construct
__construct(array $params=[], array $files=[], $boundary=null)
Definition: RequestBodyMultipart.php:59
Facebook\FileUpload\FacebookFile
Definition: FacebookFile.php:34