Twitter API: Url search for Username

后端 未结 2 738
梦毁少年i
梦毁少年i 2021-01-17 04:56

I have a list of websites where I want to see if they have twitter accounts. I was curious if there is a url search for username in the API, or something of this nature. I\'

相关标签:
2条回答
  • 2021-01-17 05:34

    Here's my code that checks if the user does exist, based on Jimbo's very useful PHP class as detailed above.

    First, use json_decode to convert $result into a PHP array. Make sure to set the second parameter to true so that you won't get Objects in the resulting array:

    $result = json_decode($result, true);
    

    Now you have to check if the screen_name array key exists, but array_key_exists doesn't recursively look through multidimensional arrays. So you'll need a new function as found here:

    function array_key_exists_r($needle, $haystack) {
        $result = array_key_exists($needle, $haystack);
    
        if ($result) return $result;
    
        foreach ($haystack as $v) {
            if (is_array($v)) {
                 $result = array_key_exists_r($needle, $v);
            }
            if ($result) return $result;
        }
    
        return $result;
    }
    

    And below is the final piece of code that makes it all work. Note where I'm pointing to in the $result array. I'm basically looking at the screen_name of the user who posted the first tweet, since the Twitter API returns the timeline of the user as specified in $username.

    $result = json_decode($result, true);
    
    if ( array_key_exists_r('screen_name', $result ) ) {
        if ( $result[0]['user']['screen_name'] == $username ) {
            // The username exists
        }
    } else {
        // The username doesn't exist
    }
    

    I'm not sure if there's a better way to check if the username exists. Maybe just use check for an error code instead?

    0 讨论(0)
  • 2021-01-17 05:35

    As explained in the comment above, version 1 of the twitter API is deprecated and will soon be removed completely. This means that simple requests will not work any more once it has been removed, which is real soon!

    Basically, from then on, in order to get any sort of data from twitter, you need to make authenticated requests using their version 1.1 api.

    I wrote a post here which explains, with pretty pictures for those who need them, the exact steps required to make calls to their version 1.1 api using PHP.

    Here's what you want to do, after you have read the above post thoroughly.

    1. Set up an application on the twitter dev site. This is simply so both you and twitter have a set of keys between each other (there are four keys in total, my post explains this).
    2. Include the PHP script, again everything is in my post and on github.
    3. From then on, you simply pass a variable username to PHP, and use that variable to check if they exist using the v1.1 api.

    I'm going to assume you know a little PHP. This is how you would use the twitter v1.1 api to check if a username exists or not.

    Take a look at the docs for getting a user's timeline. The docs state you can use a screen_name parameter. You also know it requires a GET request.


    Armed with the above information, and my class, you can perform a request for a user pretty easily.

    require_once "TwitterAPIExchange.php";
    
    // As my post explains, these are the keys you get from the twitter dev site
    $settings = array(
        'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
        'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
        'consumer_key' => "YOUR_CONSUMER_KEY",
        'consumer_secret' => "YOUR_CONSUMER_SECRET"
    );
    
    // This is the username you want to check. You can get it however you want. Just place it into this $username variable.
    $username = 'USERNAMEHERE';
    
    $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
    $requestMethod = 'GET';
    $getfield = '?screen_name='.$username;
    
    $twitter = new TwitterAPIExchange($settings);
    $result = $twitter->setGetfield($getfield)
                      ->buildOauth($url, $requestMethod)
                      ->performRequest();
    

    Okay, so the result is now stored in the $result variable. You can do what you want here. You want to check the user exists, so var_dump() the result and look for how to figure out if the user exists or not. Clearly it won't contain an actual user, it may contain false or null.

    As I don't know off the top of my head, lets say $result->user is equal to false. You would simply do this:

    if (!$result->user)
    {
        echo "The user doesn't exist!";
    }
    else
    {
        echo "The user exists!";
    }
    

    ... or, short-hand style (I like adding little things like this to my posts)

    echo (!$result->user) ? "Doesn't exist! :(" : "Exists!";
    

    Do a little research, using:

    • My post here
    • The class on github
    • The twitter dev api
    0 讨论(0)
提交回复
热议问题