How to unfollow some friend who sign up my app twitter

541 views Asked by At

I want to follow/unfollow some friends who authorize my Twitter app.

For example:

User signs in with my twitter application and he wants to follow some people

How does it work? I wrote some code here but not working, The session works fine, The user signs in but create/friendship not working, why?

<?php
session_start();
require_once('TwitterAPIExchange.php');
require_once('tmhOAuth.php');
require_once('tmhUtilities.php');
require_once('twitteroauth.php');
require 'twconfig.php';
echo $_SESSION['oauth_token'];
echo "<br />";
echo $_SESSION['oauth_token_secret'];

$twitteroauth = new TwitterOAuth($consumerKey, $consumerKeySecret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret'] );
$twitteroauth->post('friendships/create', array('screen_name' => 'savanpaun'));
?>

Simply put, I want people follow/unfollow friends using signup in my application directly.

2

There are 2 answers

5
archish On

here is a sample code to follow someone.I’ve had used Abraham library you can get it from here https://github.com/abraham/twitteroauth.

also this is a twitter documentation you can check it out https://dev.twitter.com/rest/reference/post/friendships/create. and to unfollow someone just use 'friendships/destroy'

<?php
echo "<pre>";
$consumerKey = 'your consumer key';
$consumerSecret = 'your consumer secret key';
$oAuthToken = 'your oauth token';
$oAuthSecret = 'your oauth secret';

require_once('twitteroauth.php');

$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);

//get friend list
$list= $tweet->post('friendships/create', array('screen_name' => 'archish9'));

var_dump(json_decode($list));
print_r($list);
?>
1
Emiliano On

I copy paste you example and replace in the last line with destroy command

<?php
session_start();
require_once('TwitterAPIExchange.php');
require_once('tmhOAuth.php');
require_once('tmhUtilities.php');
require_once('twitteroauth.php');
require 'twconfig.php';
echo $_SESSION['oauth_token'];
echo "<br />";
echo $_SESSION['oauth_token_secret'];

$twitteroauth = new TwitterOAuth($consumerKey, $consumerKeySecret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret'] );
$twitteroauth->post('friendships/destroy', array('user_id' => 'iduser'));
?>

this line

$twitteroauth->post('friendships/destroy', array('user_id' => 'iduser'));

I hope that this help you in some way

best