Skip to content

Instantly share code, notes, and snippets.

@nickdenardis
Created December 31, 2010 14:45
Show Gist options
  • Save nickdenardis/761063 to your computer and use it in GitHub Desktop.
Save nickdenardis/761063 to your computer and use it in GitHub Desktop.
Get a list of the recent answers for a user in Formspring
// Get the last question in the DB for this user
$this->ResetValues();
$this->SetValue('user_id', $user_id);
$last_question = current($this->GetAssoc('question_id', 'question_id', 'DESC', 0, 1));
// Start the API connection
$connection = new FormspringOAuth(CONSUMER_KEY, CONSUMER_SECRET,
$myUser->GetValue('oauth_token'), $myUser->GetValue('oauth_token_secret'));
// Grab new items in the inbox
$inbox = $connection->get('answered/list/' . $myUser->GetValue('username'), array('since_id' => (int)$last_question));
$max_id = 0;
$total_imported = 0;
do {
// If we have to get older items
if ($max_id != 0)
$inbox = $connection->get('answered/list/' . $myUser->GetValue('username'), array('max_id' => $max_id));
// Loop through and make sure the questions are in the database
if (is_array($inbox->response))
foreach ($inbox->response as $answer){
if ($answer->id > (int)$last_question){
// Set all the values for the database
$this->ResetValues();
$this->SetValue('question_id', $answer->id);
$this->SetValue('question', $answer->question);
$this->SetValue('answer', $answer->answer);
$this->SetValue('date_asked', date('Y-m-d H:i:s', strtotime($answer->time)));
$this->SetValue('user_id', $myUser->GetPrimary());
// Save the new answer
if (!$this->Save())
throw new SimplException('Error saving answer to database', 2, 'Error: Saving answer to database: ' . $answer->id);
// Increment the number of imported items
$total_imported++;
}
// Set the new max id
$max_id = $answer->id;
}
}while ($max_id != 0 && is_array($inbox->response) && count($inbox->response) > 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment