GithubHelp home page GithubHelp logo

facebook-batch-api's Introduction

facebook-batch-api

The goal is to use the Facebook SDK via batch requests without having to complexify the code with the pre-processing and post-processing phases.

The trick is to use reference to return a temporary placeholder and to replace the placeholder with the real result when needed.

Usage :

// use the api as usual, please note the &
$me = &$facebook->batchApi('/me');
$picture = &$facebook->batchApi('/me/picture', 'GET', [
    'type' => 'large',
    'return_ssl_resources' => 1,
]);

// execute the batch to fill $me and $picture with the real value
$facebook->processBatch();

// use the results
var_dump($me);
var_dump($picture);

Batch requests containing multiple methods

curl \
    -F 'access_token=…' \
    -F 'batch=[{ "method":"POST","relative_url":"me/feed","body":"message=Test status update&link=https://developers.facebook.com/"},{ "method":"GET","relative_url":"me/feed?limit=1"}]' \
    https://graph.facebook.com

The second parameter makes it easy to alternate methods.

$result1 = &$facebook->batchApi('me/feed', 'POST', [
    'message' => 'Test status update',
    'link' => 'https://developers.facebook.com/',
]);
$result2 = &$facebook->batchApi('me/feed', 'GET', [
    'limit' => 1,
]);
$facebook->processBatch();

Specifying dependencies between operations in the request

curl \
   -F 'access_token=...' \
   -F 'batch=[{ "method":"GET","name":"get-friends","relative_url":"me/friends?limit=5",},{"method":"GET","relative_url":"?ids={result=get-friends:$.data.*.id}"}]' \
   https://graph.facebook.com/

A batch request can be named with the fourth parameter for later use.

$result1 = &$facebook->batchApi('me/friends', 'GET', [
    'limit' => 5,
], [
    'name' => 'get-friends',
]);
$result2 = &$facebook->batchApi('/', 'GET', [
    'ids' => '{result=get-friends:$.data.*.id}',
]);
$facebook->processBatch();

Uploading binary data

curl 
     -F 'access_token=…' \
     -F 'batch=[{"method":"POST","relative_url":"me/photos","body":"message=My cat photo","attached_files":"file1"},{"method":"POST","relative_url":"me/photos","body":"message=My dog photo","attached_files":"file2"},]' \
     -F '[email protected]' \
     -F '[email protected]' \
    https://graph.facebook.com

The method attachFile can be used to attach a file. The correspondance between the file name and the batch parameter is made internally.

$result1 = &$facebook->batchApi('me/photos', 'POST', [
    'message' => 'My cat photo',
], [
    'attached_files' => $facebook->attachFile('@cat.gif'),
]);
$result2 = &$facebook->batchApi('me/photos', 'POST', [
    'message' => 'My dog photo',
], [
    'attached_files' => $facebook->attachFile('@dog.jpg'),
]);
$facebook->processBatch();

facebook-batch-api's People

Contributors

vlechemin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.