Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit 4980f3a

Browse files
committed
Fix batch examples to comply with PSR2
1 parent 16a6a0c commit 4980f3a

File tree

1 file changed

+50
-47
lines changed

1 file changed

+50
-47
lines changed

docs/examples/batch_request.md

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ This example covers sending a batch request with the Facebook SDK for PHP.
77
The following example assumes we have the following permissions granted from the user: `user_likes`, `user_events`, `user_photos`, `publish_actions`. The example makes use of [JSONPath to reference specific batch operations](https://developers.facebook.com/docs/graph-api/making-multiple-requests/#operations).
88

99
```php
10+
<?php
1011
$fb = new Facebook\Facebook([
11-
'app_id' => '{app-id}',
12-
'app_secret' => '{app-secret}',
13-
'default_graph_version' => 'v2.8',
14-
]);
12+
'app_id' => '{app-id}',
13+
'app_secret' => '{app-secret}',
14+
'default_graph_version' => 'v2.8',
15+
]);
1516

1617
// Since all the requests will be sent on behalf of the same user,
1718
// we'll set the default fallback access token here.
@@ -51,29 +52,30 @@ $batch = [
5152
echo '<h1>Make a batch request</h1>' . "\n\n";
5253

5354
try {
54-
$responses = $fb->sendBatchRequest($batch);
55-
} catch(Facebook\Exceptions\FacebookResponseException $e) {
56-
// When Graph returns an error
57-
echo 'Graph returned an error: ' . $e->getMessage();
58-
exit;
59-
} catch(Facebook\Exceptions\FacebookSDKException $e) {
60-
// When validation fails or other local issues
61-
echo 'Facebook SDK returned an error: ' . $e->getMessage();
62-
exit;
55+
$responses = $fb->sendBatchRequest($batch);
56+
} catch (Facebook\Exceptions\FacebookResponseException $e) {
57+
// When Graph returns an error
58+
echo 'Graph returned an error: ' . $e->getMessage();
59+
exit;
60+
} catch (Facebook\Exceptions\FacebookSDKException $e) {
61+
// When validation fails or other local issues
62+
echo 'Facebook SDK returned an error: ' . $e->getMessage();
63+
exit;
6364
}
6465

6566
foreach ($responses as $key => $response) {
66-
if ($response->isError()) {
67-
$e = $response->getThrownException();
68-
echo '<p>Error! Facebook SDK Said: ' . $e->getMessage() . "\n\n";
69-
echo '<p>Graph Said: ' . "\n\n";
70-
var_dump($e->getResponse());
71-
} else {
72-
echo "<p>(" . $key . ") HTTP status code: " . $response->getHttpStatusCode() . "<br />\n";
73-
echo "Response: " . $response->getBody() . "</p>\n\n";
74-
echo "<hr />\n\n";
75-
}
67+
if ($response->isError()) {
68+
$e = $response->getThrownException();
69+
echo '<p>Error! Facebook SDK Said: ' . $e->getMessage() . "\n\n";
70+
echo '<p>Graph Said: ' . "\n\n";
71+
var_dump($e->getResponse());
72+
} else {
73+
echo "<p>(" . $key . ") HTTP status code: " . $response->getHttpStatusCode() . "<br />\n";
74+
echo "Response: " . $response->getBody() . "</p>\n\n";
75+
echo "<hr />\n\n";
76+
}
7677
}
78+
7779
```
7880

7981
There five requests being made in this batch requests.
@@ -235,41 +237,42 @@ foreach ($responses as $key => $response) {
235237
Since the requests sent in a batch are unrelated by default, we can make requests on behalf of multiple users and pages in the same batch request.
236238

237239
```php
240+
<?php
238241
$fb = new Facebook\Facebook([
239-
'app_id' => '{app-id}',
240-
'app_secret' => '{app-secret}',
241-
'default_graph_version' => 'v2.8',
242-
]);
242+
'app_id' => '{app-id}',
243+
'app_secret' => '{app-secret}',
244+
'default_graph_version' => 'v2.8',
245+
]);
243246

244247
$batch = [
245248
$fb->request('GET', '/me?fields=id,name', 'user-access-token-one'),
246249
$fb->request('GET', '/me?fields=id,name', 'user-access-token-two'),
247250
$fb->request('GET', '/me?fields=id,name', 'page-access-token-one'),
248251
$fb->request('GET', '/me?fields=id,name', 'page-access-token-two'),
249-
];
252+
];
250253

251254
try {
252-
$responses = $fb->sendBatchRequest($batch);
253-
} catch(Facebook\Exceptions\FacebookResponseException $e) {
254-
// When Graph returns an error
255-
echo 'Graph returned an error: ' . $e->getMessage();
256-
exit;
257-
} catch(Facebook\Exceptions\FacebookSDKException $e) {
258-
// When validation fails or other local issues
259-
echo 'Facebook SDK returned an error: ' . $e->getMessage();
260-
exit;
255+
$responses = $fb->sendBatchRequest($batch);
256+
} catch (Facebook\Exceptions\FacebookResponseException $e) {
257+
// When Graph returns an error
258+
echo 'Graph returned an error: ' . $e->getMessage();
259+
exit;
260+
} catch (Facebook\Exceptions\FacebookSDKException $e) {
261+
// When validation fails or other local issues
262+
echo 'Facebook SDK returned an error: ' . $e->getMessage();
263+
exit;
261264
}
262265

263266
foreach ($responses as $key => $response) {
264-
if ($response->isError()) {
265-
$e = $response->getThrownException();
266-
echo '<p>Error! Facebook SDK Said: ' . $e->getMessage() . "\n\n";
267-
echo '<p>Graph Said: ' . "\n\n";
268-
var_dump($e->getResponse());
269-
} else {
270-
echo "<p>(" . $key . ") HTTP status code: " . $response->getHttpStatusCode() . "<br />\n";
271-
echo "Response: " . $response->getBody() . "</p>\n\n";
272-
echo "<hr />\n\n";
273-
}
267+
if ($response->isError()) {
268+
$e = $response->getThrownException();
269+
echo '<p>Error! Facebook SDK Said: ' . $e->getMessage() . "\n\n";
270+
echo '<p>Graph Said: ' . "\n\n";
271+
var_dump($e->getResponse());
272+
} else {
273+
echo "<p>(" . $key . ") HTTP status code: " . $response->getHttpStatusCode() . "<br />\n";
274+
echo "Response: " . $response->getBody() . "</p>\n\n";
275+
echo "<hr />\n\n";
276+
}
274277
}
275278
```

0 commit comments

Comments
 (0)