markwilson/three-legged-oauth

此包已被废弃且不再维护。未建议替代包。

Oauth应用基础

0.1.10 2015-04-06 13:30 UTC

README

安装

通过Composer

markwilson/three-legged-oauth 添加到您的 composer.json 依赖中。

php composer.phar install

使用方法

<?php

require_once 'vendor/autoload.php';

$session = new Symfony\Component\HttpFoundation\Session\Session();

$oAuth = new MarkWilson\ThreeLeggedOAuth(
    '<base oauth url>',
    '<consumer key>',
    '<consumer secret>',
    $session,
    '<base app url>'
);

if ($oauth->isAuthorised()) {
    // already authorised, let's connect to the app
    $response = $oauth->get('<app endpoint>');

    echo $response;
} elseif ($oauth->isPendingAuthorisation()) {
    // waiting for authorisation, request access
    $oauth->getAccessToken();
} else {
    // no authorisation yet, start request
    // optionally pass through the callback url here
    $oauth->requestToken('http://...');
}

Twitter首页时间线(非常)基本示例

<?php

require_once 'vendor/autoload.php';

$session = new Symfony\Component\HttpFoundation\Session\Session();

$oauth = new MarkWilson\ThreeLeggedOAuth(
    'https://api.twitter.com/oauth/',
    '<consumer key>',
    '<consumer secret>',
    $session,
    'https://api.twitter.com/1.1'
);

if ($oauth->isAuthorised()) {
    $response = $oauth->get('/statuses/home_timeline.json');

    $jsonDecoded = json_decode($response);

    foreach ($jsonDecoded as $tweet) {
        echo $tweet->user->name . ': ' . $tweet->text . '<br />';
    }
} elseif ($oauth->isPendingAuthorisation()) {
    $oauth->getAccessToken();
} else {
    $oauth->requestToken('http://' . $_SERVER['HTTP_HOST'] . '/');
}

待办事项列表

  • 移除Symfony会话依赖
  • 包含接口以在请求之间持久化令牌