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

Commit 16a6a0c

Browse files
committed
Add example for 'depends_on' option
1 parent 7f8685f commit 16a6a0c

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

docs/examples/batch_request.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,75 @@ foreach ($responses as $key => $response) {
161161

162162
```
163163

164+
## Explicit Dependency Example
165+
166+
In the following example we will make two requests.
167+
* One to post a status update on the user's feed
168+
* and one to receive the last post of the user (which should be the one that we posted with first request).
169+
170+
Since we want the second request to be executed after the first one is completed, we have to set the `depends_on` option of the second request to point to the name of the first request. We assume that we have the following options granted from the user: `user_posts`, `publish_actions`.
171+
172+
```php
173+
<?php
174+
$fb = new Facebook\Facebook([
175+
'app_id' => '{app-id}',
176+
'app_secret' => '{app-secret}',
177+
'default_graph_version' => 'v2.8',
178+
]);
179+
180+
// Since all the requests will be sent on behalf of the same user,
181+
// we'll set the default fallback access token here.
182+
$fb->setDefaultAccessToken('user-access-token');
183+
184+
// Post a status update to the user's feed
185+
$message = 'Random status update';
186+
$statusUpdate = ['message' => $message];
187+
$requestPostToFeed = $fb->request('POST', '/me/feed', $statusUpdate);
188+
189+
// Get last post of the user
190+
$requestLastPost = $fb->request('GET', '/me/feed?limit=1');
191+
192+
// Create an empty batch request
193+
$batch = $fb->newBatchRequest();
194+
195+
// Populate the batch request
196+
$batch->add($requestPostToFeed, "post-to-feed");
197+
198+
// Set the 'depends_on' property to point to the first request
199+
$batch->add($requestLastPost, [
200+
"name" => "last-post",
201+
"depends_on" => "post-to-feed"
202+
]);
203+
204+
// Send the batch request
205+
try {
206+
$responses = $fb->getClient()->sendBatchRequest($batch);
207+
} catch (Facebook\Exceptions\FacebookResponseException $e) {
208+
// When Graph returns an error
209+
echo 'Graph returned an error: ' . $e->getMessage();
210+
exit;
211+
} catch (Facebook\Exceptions\FacebookSDKException $e) {
212+
// When validation fails or other local issues
213+
echo 'Facebook SDK returned an error: ' . $e->getMessage();
214+
exit;
215+
}
216+
217+
foreach ($responses as $key => $response) {
218+
if ($response->isError()) {
219+
$e = $response->getThrownException();
220+
echo '<p>Error! Facebook SDK Said: ' . $e->getMessage() . "\n\n";
221+
echo '<p>Graph Said: ' . "\n\n";
222+
var_dump($e->getResponse());
223+
} else {
224+
echo "<p>(" . $key . ") HTTP status code: " . $response->getHttpStatusCode() . "<br />\n";
225+
echo "Response: " . $response->getBody() . "</p>\n\n";
226+
echo "<hr />\n\n";
227+
}
228+
}
229+
```
230+
231+
> **Warning:** The response object should return a `null` response for any request that was pointed to with the `depends_on` option 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 example](#force-response-example).
232+
164233
## Multiple User Example
165234

166235
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.

0 commit comments

Comments
 (0)