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!

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

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理