dtcd27183 2015-02-26 18:45
浏览 23

检查Twitter帐户是否存在

I need to check if the twitter name user puts to the form exists or not.

I am using TwitterOAuth by Abraham Williams to do that, but I always get just false...

Here is the code:

require_once 'includes/twitteroauth/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;

define('CONSUMER_KEY', ' ');
define('CONSUMER_SECRET', '');
define('ACCESS_TOKEN', '');
define('ACCESS_TOKEN_SECRET', '');

function find_users(array $users){
    $founds = array();
    $toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

    // Up to 100 users per request.
    $userAry = array_slice($users, 0, 100);

    // Init with "not found" for all users.
    foreach ($userAry as $user) {
        $founds[$user] = false;
    }

    // Find existing users by "screen_name".
    $userObjs= $toa->post('users/lookup', array('screen_name' => implode(',', $userAry)));

    // Set "found" for existing users.
    foreach ($userObjs as $userObj) {
        $founds[$userObj->screen_name] = true;
    }

    return $founds;
}

Here is my call (I put there some names I know exist and also their register names, just to be sure):

if ($pass AND !empty($twitter)){
    $twitter_array = array($twitter, "Jayztwocents", "Barnacules", "Barnacules Nerdgasm", "Inu-ki", "Inukii", "HiRezStew", "schisam", "twitter", "twitterapi");
    $check_twitter = find_users($twitter_array);
    var_dump($check_twitter);
    die;
    if (!$check_twitter[0]){
        $message .= "<li>This twitter account does not exist.</li>";    $pass = FALSE;  $twitter_class = "required";
    }
}

This is the result of my var_dump:

array(11) {
    ["MiChAeLoKGB"]=> bool(false)
    ["Jayztwocents"]=> bool(false)
    ["Barnacules"]=> bool(false)
    ["Barnacules Nerdgasm"]=> bool(false)
    ["Inu-ki"]=> bool(false)
    ["Inukii"]=> bool(false)
    ["HiRezStew"]=> bool(false)
    ["schisam"]=> bool(false)
    ["twitter"]=> bool(false)
    ["twitterapi"]=> bool(false)
    [""]=> bool(true) 
}

Why does it always return false, and why the heck is as the end emty string thats true?

Ps.: All the keys/secrets are correctly set up.

PPs.: I googled a lot and there are questions about it on this site, but they all use old API which is disabled.

  • 写回答

1条回答 默认 最新

  • douhao2856 2015-02-26 22:50
    关注

    Ehm, I found out that I needed to create my token again and now it works well :)

    Anyway, for my needs I needed to get only single user and I needed it to work even if twitter name is written with wrong caps (MYtwiTTERnAme instead of MyTwitterName), so I changed the function a bit.

    Here it is, if somebody needs something like that:

    function find_users($user){
        $toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
    
        $userObjs= $toa->post('users/lookup', array('screen_name' => $user));
    
        if (isset($userObjs->errors)){
            return FALSE;
        }elseif (strtolower($userObjs[0]->screen_name) == strtolower($user)){
            return $userObjs[0]->screen_name;
        }else{
            return FALSE;
        }
    }
    

    And heres how you call it and test it:

    $check_twitter = find_users($twitter);
    if ($check_twitter === FALSE){
        $message .= "<li>This twitter account does not exist.</li>";    $pass = FALSE;  $twitter_class = "required";
    }else{
        $twitter = $check_twitter;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题