Skip to content

Instantly share code, notes, and snippets.

@agustinhaller
Created September 28, 2012 16:43
Show Gist options
  • Save agustinhaller/3800861 to your computer and use it in GitHub Desktop.
Save agustinhaller/3800861 to your computer and use it in GitHub Desktop.
follow
function followUser($user_id, $tw_username_to_follow)
{
$response_array = array();
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => TWITTER_CONSUMER_KEY,
'consumer_secret' => TWITTER_CONSUMER_SECRET,
));
$response_array = array(
'response' => "ERROR",
'response_msg' => "Could not connect to db"
);
// Get access tokens of the user from the db
// Connect to db
$conn = connect_db();
$query_ok = false;
if($conn!=false)
{
$q_a_t = "SELECT oauth_token, oauth_token_secret
FROM users_accounts
WHERE id = ".$user_id."
AND social_network = 'TWITTER'";
$r_a_t = mysql_query($q_a_t);
if($r_a_t)
{
if(mysql_num_rows($r_a_t) > 0)
{
$a_t = mysql_fetch_array($r_a_t);
$tmhOAuth->config['user_token'] = $a_t['oauth_token'];
$tmhOAuth->config['user_secret'] = $a_t['oauth_token_secret'];
$query_ok = true;
}
else// No results returned from db
{
// Lets build the response array
$response_array = array(
'response' => "ERROR",
'response_msg' => "No user found in db"
);
}
}
else// Error in MySql result
{
// Lets build the response array
$response_array = array(
'response' => "ERROR",
'response_msg' => "Error in MySql result"
);
}
}
else
{
// Error in mysql connection
}
// Destroy db connection
disconnect_db($conn);
if($query_ok)
{
$tmhOAuth->request('POST', $tmhOAuth->url('1/friendships/create'), array(
//'user_id' => $target_user
'screen_name' => $tw_username_to_follow
));
if($tmhOAuth->response['code'] == 200)
{
// Lets build the response array
$response_array = array(
'response' => "OK",
'response_msg' => "Like a charm"
);
}
else// Twitter call error
{
// echo("<br/><br/>");
$tw_response = $tmhOAuth->response['response'];
// var_dump($tw_response);
// echo("<br/><br/>");
// Lets build the response array
$response_array = array(
'response' => "ERROR",
'response_msg' => "Twitter call error",
'tw_response' => $tw_response
);
}
}
return $response_array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment