2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

一方的フォローを取り消すプログラムをつくった

Posted at

!注意!

現在、APIの使用は変更されています。

「そういえば、無料で利用できたいい時代もあったなぁ」

という感覚で、ご覧ください。

<?php

// Twitter APIのライブラリをインストール
require_once('autoload.php');
use Abraham\TwitterOAuth\TwitterOAuth;

// Twitter APIの認証情報を設定
$consumer_key = '';
$consumer_secret = '';
$access_token = '';
$access_token_secret = '';

// Twitter APIに接続
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);

// 自分のアカウント情報を取得
$user = $connection->get('account/verify_credentials');

// 自分がフォローしているアカウントのIDを取得
$following_ids = $connection->get('friends/ids', ['screen_name' => $user->screen_name])->ids;

// 自分をフォローしているアカウントのIDを取得
$follower_ids = $connection->get('followers/ids', ['screen_name' => $user->screen_name])->ids;

// 自分をフォローしていないアカウントのIDを取得
$unfollowing_ids = array_diff($following_ids, $follower_ids);

// 自分をフォローしていないアカウントのフォローを解除
foreach ($unfollowing_ids as $unfollowing_id) {
    try {
        $connection->post('friendships/destroy', ['user_id' => $unfollowing_id]);
        echo "Unfollowed user with ID: " . $unfollowing_id . "\n";
    } catch (Exception $e) {
        error_log('Error unfollowing user with ID ' . $unfollowing_id . ': ' . $e->getMessage());
    }
}

?>
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?