Ejemplo n.º 1
0
        echo 'rate limited. reset time is ' . $reset . PHP_EOL;
        echo 'sleeping for ' . $sleep . ' seconds';
        sleep($sleep);
    }
}
require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
$screen_name = 'tmhoauth';
$cursor = '-1';
$friends = array();
while (true) {
    if ($cursor == '0') {
        break;
    }
    echo '.';
    $code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/friends/list'), array('cursor' => $cursor, 'skip_status' => true, 'screen_name' => $screen_name));
    check_rate_limit($tmhOAuth->response);
    if ($code == 200) {
        $data = json_decode($tmhOAuth->response['response'], true);
        $friends = array_merge($friends, $data['users']);
        $cursor = $data['next_cursor_str'];
    } else {
        $tmhOAuth->render_response();
        break;
    }
    usleep(500000);
}
echo '@' . $screen_name . ' follows ' . count($friends) . ' users.' . PHP_EOL . PHP_EOL;
foreach ($friends as $friend) {
    echo '@' . $friend['screen_name'] . ' (' . $friend['id_str'] . ')' . PHP_EOL;
}
Ejemplo n.º 2
0
<?php

// this example follows the guidance given on dev.twitter.com for obtaining an app-only bearer token
require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
$bearer = $tmhOAuth->bearer_token_credentials();
$params = array('grant_type' => 'client_credentials');
$code = $tmhOAuth->request('POST', $tmhOAuth->url('/oauth2/token', null), $params, false, false, array('Authorization' => "Basic {$bearer}"));
if ($code == 200) {
    $data = json_decode($tmhOAuth->response['response']);
    if (isset($data->token_type) && strcasecmp($data->token_type, 'bearer') === 0) {
        $new_bearer = $data->access_token;
        echo 'Your app-only bearer token is: ' . $new_bearer . PHP_EOL;
    }
} else {
    $tmhOAuth->render_response();
}