FacebookResponse.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. /**
  3. * Copyright 2016 Facebook, Inc.
  4. *
  5. * You are hereby granted a non-exclusive, worldwide, royalty-free license to
  6. * use, copy, modify, and distribute this software in source code or binary
  7. * form for use in connection with the web services and APIs provided by
  8. * Facebook.
  9. *
  10. * As with any software that integrates with the Facebook platform, your use
  11. * of this software is subject to the Facebook Developer Principles and
  12. * Policies [http://developers.facebook.com/policy/]. This copyright notice
  13. * shall be included in all copies or substantial portions of the software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. namespace Facebook;
  25. use Facebook\GraphNodes\GraphNodeFactory;
  26. use Facebook\Exceptions\FacebookResponseException;
  27. use Facebook\Exceptions\FacebookSDKException;
  28. /**
  29. * Class FacebookResponse
  30. *
  31. * @package Facebook
  32. */
  33. class FacebookResponse
  34. {
  35. /**
  36. * @var int The HTTP status code response from Graph.
  37. */
  38. protected $httpStatusCode;
  39. /**
  40. * @var array The headers returned from Graph.
  41. */
  42. protected $headers;
  43. /**
  44. * @var string The raw body of the response from Graph.
  45. */
  46. protected $body;
  47. /**
  48. * @var array The decoded body of the Graph response.
  49. */
  50. protected $decodedBody = [];
  51. /**
  52. * @var FacebookRequest The original request that returned this response.
  53. */
  54. protected $request;
  55. /**
  56. * @var FacebookSDKException The exception thrown by this request.
  57. */
  58. protected $thrownException;
  59. /**
  60. * Creates a new Response entity.
  61. *
  62. * @param FacebookRequest $request
  63. * @param string|null $body
  64. * @param int|null $httpStatusCode
  65. * @param array|null $headers
  66. */
  67. public function __construct(FacebookRequest $request, $body = null, $httpStatusCode = null, array $headers = [])
  68. {
  69. $this->request = $request;
  70. $this->body = $body;
  71. $this->httpStatusCode = $httpStatusCode;
  72. $this->headers = $headers;
  73. $this->decodeBody();
  74. }
  75. /**
  76. * Return the original request that returned this response.
  77. *
  78. * @return FacebookRequest
  79. */
  80. public function getRequest()
  81. {
  82. return $this->request;
  83. }
  84. /**
  85. * Return the FacebookApp entity used for this response.
  86. *
  87. * @return FacebookApp
  88. */
  89. public function getApp()
  90. {
  91. return $this->request->getApp();
  92. }
  93. /**
  94. * Return the access token that was used for this response.
  95. *
  96. * @return string|null
  97. */
  98. public function getAccessToken()
  99. {
  100. return $this->request->getAccessToken();
  101. }
  102. /**
  103. * Return the HTTP status code for this response.
  104. *
  105. * @return int
  106. */
  107. public function getHttpStatusCode()
  108. {
  109. return $this->httpStatusCode;
  110. }
  111. /**
  112. * Return the HTTP headers for this response.
  113. *
  114. * @return array
  115. */
  116. public function getHeaders()
  117. {
  118. return $this->headers;
  119. }
  120. /**
  121. * Return the raw body response.
  122. *
  123. * @return string
  124. */
  125. public function getBody()
  126. {
  127. return $this->body;
  128. }
  129. /**
  130. * Return the decoded body response.
  131. *
  132. * @return array
  133. */
  134. public function getDecodedBody()
  135. {
  136. return $this->decodedBody;
  137. }
  138. /**
  139. * Get the app secret proof that was used for this response.
  140. *
  141. * @return string|null
  142. */
  143. public function getAppSecretProof()
  144. {
  145. return $this->request->getAppSecretProof();
  146. }
  147. /**
  148. * Get the ETag associated with the response.
  149. *
  150. * @return string|null
  151. */
  152. public function getETag()
  153. {
  154. return isset($this->headers['ETag']) ? $this->headers['ETag'] : null;
  155. }
  156. /**
  157. * Get the version of Graph that returned this response.
  158. *
  159. * @return string|null
  160. */
  161. public function getGraphVersion()
  162. {
  163. return isset($this->headers['Facebook-API-Version']) ? $this->headers['Facebook-API-Version'] : null;
  164. }
  165. /**
  166. * Returns true if Graph returned an error message.
  167. *
  168. * @return boolean
  169. */
  170. public function isError()
  171. {
  172. return isset($this->decodedBody['error']);
  173. }
  174. /**
  175. * Throws the exception.
  176. *
  177. * @throws FacebookSDKException
  178. */
  179. public function throwException()
  180. {
  181. throw $this->thrownException;
  182. }
  183. /**
  184. * Instantiates an exception to be thrown later.
  185. */
  186. public function makeException()
  187. {
  188. $this->thrownException = FacebookResponseException::create($this);
  189. }
  190. /**
  191. * Returns the exception that was thrown for this request.
  192. *
  193. * @return FacebookSDKException|null
  194. */
  195. public function getThrownException()
  196. {
  197. return $this->thrownException;
  198. }
  199. /**
  200. * Convert the raw response into an array if possible.
  201. *
  202. * Graph will return 2 types of responses:
  203. * - JSON(P)
  204. * Most responses from Graph are JSON(P)
  205. * - application/x-www-form-urlencoded key/value pairs
  206. * Happens on the `/oauth/access_token` endpoint when exchanging
  207. * a short-lived access token for a long-lived access token
  208. * - And sometimes nothing :/ but that'd be a bug.
  209. */
  210. public function decodeBody()
  211. {
  212. $this->decodedBody = json_decode($this->body, true);
  213. if ($this->decodedBody === null) {
  214. $this->decodedBody = [];
  215. parse_str($this->body, $this->decodedBody);
  216. } elseif (is_bool($this->decodedBody)) {
  217. // Backwards compatibility for Graph < 2.1.
  218. // Mimics 2.1 responses.
  219. // @TODO Remove this after Graph 2.0 is no longer supported
  220. $this->decodedBody = ['success' => $this->decodedBody];
  221. } elseif (is_numeric($this->decodedBody)) {
  222. $this->decodedBody = ['id' => $this->decodedBody];
  223. }
  224. if (!is_array($this->decodedBody)) {
  225. $this->decodedBody = [];
  226. }
  227. if ($this->isError()) {
  228. $this->makeException();
  229. }
  230. }
  231. /**
  232. * Instantiate a new GraphObject from response.
  233. *
  234. * @param string|null $subclassName The GraphNode subclass to cast to.
  235. *
  236. * @return \Facebook\GraphNodes\GraphObject
  237. *
  238. * @throws FacebookSDKException
  239. *
  240. * @deprecated 5.0.0 getGraphObject() has been renamed to getGraphNode()
  241. * @todo v6: Remove this method
  242. */
  243. public function getGraphObject($subclassName = null)
  244. {
  245. return $this->getGraphNode($subclassName);
  246. }
  247. /**
  248. * Instantiate a new GraphNode from response.
  249. *
  250. * @param string|null $subclassName The GraphNode subclass to cast to.
  251. *
  252. * @return \Facebook\GraphNodes\GraphNode
  253. *
  254. * @throws FacebookSDKException
  255. */
  256. public function getGraphNode($subclassName = null)
  257. {
  258. $factory = new GraphNodeFactory($this);
  259. return $factory->makeGraphNode($subclassName);
  260. }
  261. /**
  262. * Convenience method for creating a GraphAlbum collection.
  263. *
  264. * @return \Facebook\GraphNodes\GraphAlbum
  265. *
  266. * @throws FacebookSDKException
  267. */
  268. public function getGraphAlbum()
  269. {
  270. $factory = new GraphNodeFactory($this);
  271. return $factory->makeGraphAlbum();
  272. }
  273. /**
  274. * Convenience method for creating a GraphPage collection.
  275. *
  276. * @return \Facebook\GraphNodes\GraphPage
  277. *
  278. * @throws FacebookSDKException
  279. */
  280. public function getGraphPage()
  281. {
  282. $factory = new GraphNodeFactory($this);
  283. return $factory->makeGraphPage();
  284. }
  285. /**
  286. * Convenience method for creating a GraphSessionInfo collection.
  287. *
  288. * @return \Facebook\GraphNodes\GraphSessionInfo
  289. *
  290. * @throws FacebookSDKException
  291. */
  292. public function getGraphSessionInfo()
  293. {
  294. $factory = new GraphNodeFactory($this);
  295. return $factory->makeGraphSessionInfo();
  296. }
  297. /**
  298. * Convenience method for creating a GraphUser collection.
  299. *
  300. * @return \Facebook\GraphNodes\GraphUser
  301. *
  302. * @throws FacebookSDKException
  303. */
  304. public function getGraphUser()
  305. {
  306. $factory = new GraphNodeFactory($this);
  307. return $factory->makeGraphUser();
  308. }
  309. /**
  310. * Convenience method for creating a GraphEvent collection.
  311. *
  312. * @return \Facebook\GraphNodes\GraphEvent
  313. *
  314. * @throws FacebookSDKException
  315. */
  316. public function getGraphEvent()
  317. {
  318. $factory = new GraphNodeFactory($this);
  319. return $factory->makeGraphEvent();
  320. }
  321. /**
  322. * Convenience method for creating a GraphGroup collection.
  323. *
  324. * @return \Facebook\GraphNodes\GraphGroup
  325. *
  326. * @throws FacebookSDKException
  327. */
  328. public function getGraphGroup()
  329. {
  330. $factory = new GraphNodeFactory($this);
  331. return $factory->makeGraphGroup();
  332. }
  333. /**
  334. * Instantiate a new GraphList from response.
  335. *
  336. * @param string|null $subclassName The GraphNode subclass to cast list items to.
  337. * @param boolean $auto_prefix Toggle to auto-prefix the subclass name.
  338. *
  339. * @return \Facebook\GraphNodes\GraphList
  340. *
  341. * @throws FacebookSDKException
  342. *
  343. * @deprecated 5.0.0 getGraphList() has been renamed to getGraphEdge()
  344. * @todo v6: Remove this method
  345. */
  346. public function getGraphList($subclassName = null, $auto_prefix = true)
  347. {
  348. return $this->getGraphEdge($subclassName, $auto_prefix);
  349. }
  350. /**
  351. * Instantiate a new GraphEdge from response.
  352. *
  353. * @param string|null $subclassName The GraphNode subclass to cast list items to.
  354. * @param boolean $auto_prefix Toggle to auto-prefix the subclass name.
  355. *
  356. * @return \Facebook\GraphNodes\GraphEdge
  357. *
  358. * @throws FacebookSDKException
  359. */
  360. public function getGraphEdge($subclassName = null, $auto_prefix = true)
  361. {
  362. $factory = new GraphNodeFactory($this);
  363. return $factory->makeGraphEdge($subclassName, $auto_prefix);
  364. }
  365. }