duansha6410 2016-03-14 12:34
浏览 58
已采纳

搜索Twitter用户并在自己的页面上显示结果

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!

  • 写回答

1条回答 默认 最新

  • doukuai3822 2016-03-15 11:37
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?