LoginSignup
1
1

More than 5 years have passed since last update.

Twitter フォローのアイコンURLのハッシュ一覧を取得

Posted at
<?php

define('ACCOUNT_ARZZUP', 0);
define('ACCOUNT_TEST', 1);
define('ACCOUNT', ACCOUNT_TEST);
require_once('./vendor/autoload.php');
require_once('./keys.php');

$to = new TwistOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET);

$hashes = get_friends_profile_image_hashes();
echo implode("\n", $hashes);

function get_friends_profile_image_hashes($screen_name = NULL) {
    $hashes = array();
    foreach (get_friends_profile_image($screen_name) as $url) {
        if (preg_match('#profile_images/\d+/(?<hash>.*)_normal#', $url, $m)) {
            $hashes[] = $m['hash'];
        } else {
            echo 'no-hits -> ' . $url . PHP_EOL;
         }
    }
    return $hashes;
}

function get_friends_profile_image($screen_name = NULL) {
    global $to;
    $param = array(
        'count' => 5000,
    );
    $res = $to->get('friends/ids', $param);
    if (isset($res->errors)) {
        var_dump($res->errors);
        return;
    }
    $urls = array();
    foreach (array_chunk($res->ids, 100) as $ids) {
        $ids_str = implode(',', $ids);
        $res = $to->get('users/lookup', array(
            'user_id' => $ids_str,
        ));
        if (isset($res->errors)) {
            var_dump($res->errors);
            return;
        }
        foreach ($res as $user) {
            $urls[] = $user->profile_image_url;
        }
    }
    return $urls;
}

keys.php
<?php
switch (ACCOUNT) {
case ACCOUNT_TEST:
    define('CONSUMER_KEY'       , '********');
    define('CONSUMER_SECRET'    , '********');
    define('OAUTH_TOKEN'        , '********');
    define('OAUTH_TOKEN_SECRET' , '********');
    break;
case ACCOUNT_ARZZUP:
    define('CONSUMER_KEY'       , '********');
    define('CONSUMER_SECRET'    , '********');
    define('OAUTH_TOKEN'        , '********');
    define('OAUTH_TOKEN_SECRET' , '********');
    break;
default:
    break;
}

TwistOAuth という TwitterAPI ラッパーを使う

  • フォロー一覧の取得('friends/ids')
  • ユーザ情報の取得('ids/lookup')
  • urlからハッシュの取得

の流れ

1
1
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
1
1