Ejemplo n.º 1
0
// check for oauth token in storage
$db = new MysqlUtil($db_host, $db_name, $db_user, $db_pass);
try {
    $results = $db->query("SELECT * FROM `oauth_tokens` \n        WHERE `local_user_id` = '%s' \n        AND `service` = 'yahoo' \n        LIMIT 0 , 1;", $local_user_id);
} catch (Exception $e) {
    printf('<pre>%s</pre>', print_r($e, true));
    die;
}
// there may be a record, but it may not have a valid token in it
if (count($results) > 0) {
    $access_token = json_decode($results[0]['token_json']);
}
// if there's a stored token, check if it's expired, and refresh if it is
if ($access_token && $access_token->expire_time < time()) {
    $oauth_app = new YahooOAuthApplication($oauth_consumer_key, $oauth_consumer_secret, $oauth_application_id);
    $access_token = $oauth_app->refreshAccessToken($access_token);
    $access_token->expire_time = time() + $access_token->expires_in;
    try {
        $results = $db->query("UPDATE `oauth_tokens` \n            SET `token_json` = '%s' \n            WHERE `service` = 'yahoo' \n            AND `local_user_id` = '%s', \n            LIMIT 1;", json_encode($access_token), $local_user_id);
    } catch (Exception $e) {
        printf('<pre>%s</pre>', print_r($e, true));
        die;
    }
}
if ($access_token) {
    $oauth_app = new YahooOAuthApplication($oauth_consumer_key, $oauth_consumer_secret, null, null, $access_token);
    //yql is awesome http://developer.yahoo.com/yql/console/?q=select%20*%20from%20social.profile%20where%20guid%3Dme
    $response = $oauth_app->yql('select * from social.profile where guid=me');
    $profile_data = $response->query->results->profile;
}
?>