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

Commit 7f8685f

Browse files
committed
Add example for 'omit_response_on_success' option
1 parent 177b2e9 commit 7f8685f

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

docs/examples/batch_request.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,70 @@ My next 2 events are House Warming Party,Some Foo Event.
9696

9797
It should also contain a response containing two photos from the user.
9898

99-
> **Warning:** The response object should return a `null` response for any request that was pointed to with JSONPath as is [the behaviour of the batch functionality of the Graph API](https://developers.facebook.com/docs/graph-api/making-multiple-requests/#operations).
99+
> **Warning:** The response object should return a `null` response for any request that was pointed to with JSONPath as is [the behaviour of the batch functionality of the Graph API](https://developers.facebook.com/docs/graph-api/making-multiple-requests/#operations). If we want to receive the response anyway we have to set the `omit_response_on_success` option to `false`. [See the example below](#force-response-example).
100+
101+
## Force Response Example
102+
103+
The following example is a subset of the [first example](#example). We will only use the `user-events` and `post-to-feed` requests of the [first example](#example), but in this case we will force the server to return the response of the `user-events` request.
104+
105+
```php
106+
<?php
107+
$fb = new Facebook\Facebook([
108+
'app_id' => '{app-id}',
109+
'app_secret' => '{app-secret}',
110+
'default_graph_version' => 'v2.8',
111+
]);
112+
113+
// Since all the requests will be sent on behalf of the same user,
114+
// we'll set the default fallback access token here.
115+
$fb->setDefaultAccessToken('user-access-token');
116+
117+
// Get user events
118+
$requestUserEvents = $fb->request('GET', '/me/events?fields=id,name&limit=2');
119+
120+
// Post a status update with reference to the user's events
121+
$message = 'My next 2 events are {result=user-events:$.data.*.name}.';
122+
$statusUpdate = ['message' => $message];
123+
$requestPostToFeed = $fb->request('POST', '/me/feed', $statusUpdate);
124+
125+
// Create an empty batch request
126+
$batch = $fb->newBatchRequest();
127+
128+
// Populate the batch request
129+
// Set the 'omit_response_on_success' option to false to force the server return the response
130+
$batch->add($requestUserEvents, [
131+
"name" => "user-events",
132+
"omit_response_on_success" => false
133+
]);
134+
$batch->add($requestPostToFeed, "post-to-feed");
135+
136+
// Send the batch request
137+
try {
138+
$responses = $fb->getClient()->sendBatchRequest($batch);
139+
} catch (Facebook\Exceptions\FacebookResponseException $e) {
140+
// When Graph returns an error
141+
echo 'Graph returned an error: ' . $e->getMessage();
142+
exit;
143+
} catch (Facebook\Exceptions\FacebookSDKException $e) {
144+
// When validation fails or other local issues
145+
echo 'Facebook SDK returned an error: ' . $e->getMessage();
146+
exit;
147+
}
148+
149+
foreach ($responses as $key => $response) {
150+
if ($response->isError()) {
151+
$e = $response->getThrownException();
152+
echo '<p>Error! Facebook SDK Said: ' . $e->getMessage() . "\n\n";
153+
echo '<p>Graph Said: ' . "\n\n";
154+
var_dump($e->getResponse());
155+
} else {
156+
echo "<p>(" . $key . ") HTTP status code: " . $response->getHttpStatusCode() . "<br />\n";
157+
echo "Response: " . $response->getBody() . "</p>\n\n";
158+
echo "<hr />\n\n";
159+
}
160+
}
161+
162+
```
100163

101164
## Multiple User Example
102165

0 commit comments

Comments
 (0)