简体   繁体   中英

Search for twitter users and display the result on own page

I have just implemented the twitter login on my page and it works perfectly. What I want to do next is a search bar for my users, where they can look up a twitter user and send a tweet with a mention. I was looking into the twitter documentation, but haven't found one working example of anything like this. I would appreciate any help!

Ok so i found the solution to my own question and want to share it to everyone who has the same problem. It's pretty easy actually, i just had to learn about the JSON principle (still a newby).

So here's my (simplified) code:



    session_start();

    require '../twitteroauth/autoload.php';

    use Abraham\TwitterOAuth\TwitterOAuth;

    define('CONSUMER_KEY', ''); // add your app consumer key between single quotes
    define('CONSUMER_SECRET', ''); // add your app consumer secret key between single quotes
    define('OAUTH_CALLBACK', 'http://yourdomain.com/callback.php'); // your app callback URL


    if (!isset($_SESSION['access_token'])) {
        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
        $request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));
        $_SESSION['oauth_token'] = $request_token['oauth_token'];
        $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

        $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
        echo $url;
    } else {
        $access_token = $_SESSION['access_token'];
        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

        $parameters = array('q' => 'chrischaos'); // this is where you put the seach-term

        $users = $connection->get('users/search', $parameters);


        foreach($users as $user) {
            $user = get_object_vars($user);
            echo $user['screen_name'];
        }

    }

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM