mute/facebook

实现了Graph API以及一些OAuth功能,用于与Facebook进行操作

v1.0.4 2015-04-02 15:14 UTC

README

实现了Graph API以及一些OAuth功能,用于与Facebook进行操作。请参见示例了解使用方法。

此库包需要PHP 5.3或更高版本。

如何使用此库

简单请求

<?php

$app = new \Mute\Facebook\App(APP_ID, APP_SECRET, APP_NAMESPACE);

// get name of a user
$response = $app->get(USER_ID, array('fields' => 'name'));
echo "user's name is " . $response['name'];

// or the full response
$response = $app->get(USER_ID, array('fields' => 'name'), null, true);
echo "user's name is " . $response['body']['name'];

// post a photo
$response = $app->post(USER_ID . '/photo', null, array(
    'source' => PHOTO_FILENAME
));

// get the fresh list friends
$response = $app->get(USER_ID . '/friends', null, null, array(
    'If-None-Match: ' . PREVIOUS_ETAG,
));

默认情况下,应用访问令牌将被自动附加。如果您需要使用自定义access_token进行请求

<?php
$data = $app->get('me', array(
    'access_token' => MY_ACCESS_TOKEN,
));
// or
$customApp = $app->getAuthenticatedGraphApi(MY_ACCESS_TOKEN);
$data = $customApp->get('me');

批量请求

<?php
// only bodies
$responses = $customApp->batch()
    ->get('me')
    ->get('me/friends', array('limit' => 50))
    ->execute();
// or
$responses = $customApp->batch(function($app) {
    $app->get('me');
    $app->get('me/friends', array('limit' => 50));
});

// for full responses, you can do
$responses = $customApp->getAuthenticatedGraphApi(MY_ACCESS_TOKEN)->batch()
    ->get('me')
    ->get('me/friends', array('limit' => 50))
    ->execute(true);
// or
$responses = $customApp->batch(function($customApp) {
    $customApp->get('me');
    $customApp->get('me/friends', array('limit' => 50));
}, true);

有时您需要更多控制http请求。为此,您可以操作选项

<?php
// fetching a paginated list of friends can very long, set up the timeout to 30 seconds
$app->setOptions('timeout', 60);
$friends = $app->get(USER_ID . '/friends', array(
    'offset' => 5000,
    'limit' => 5000,
));
// once finished, you can reset the options
$app->resetOptions();

API将默认调用默认Graph API版本。您可能需要更改版本

<?php
$app = $app->changeVersion('v2.0');

更多

为了生成API文档,请安装apigen,然后运行

$ apigen -c apigen.neon

为了运行单元测试,请安装PHPUnit,然后运行

$ phpunit -c tests/phpunit.xml