Search code examples
twitter-bootstraptwittertwitter-oauth

Twitter API: Url search for Username


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've been reading and looking around; however, I've come up short. I would hate to have to do this manually when I could run a function to do the work for me. Would greatly appreciate some feedback on this topic.

Good day!


Solution

  • 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: