如何在HTML页面中包含我的Twitter时间轴?

时间:2019-07-08 00:12:26

标签: javascript twitter

如何使用twitter API获取我的时间轴并将其显示在html页面中?我想使用JavaScript查询API。

我在stackoverflow上发现的问题已经过时,答案似乎不再起作用。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

正如我在评论中提到的那样,您可以在经过this tool之后使用Twitter提供的脚本。它依靠quite large script(最小为95 kB)在时间线上加载更新,这与使用API​​加载数据的区别在于此。

所提供的代码非常简单,因为您只需要一个“链接”即可将其替换为Twitter的代码。

<script async src='https://platform.twitter.com/widgets.js' charset='utf-8'></script>
<a class='twitter-timeline' href='https://twitter.com/AP?ref_src=twsrc%5Etfw'></a>

您还可以通过小部件生成器设置一些基本的自定义设置,例如宽度,高度和颜色。

(我尝试将其作为摘要,但在堆栈溢出时失败)

不幸的是,这似乎是唯一官方支持的加载时间线的方法,没有通过API通过后端和实时更新来实现它的时间。

注意:我已经亲自联系了Twitter 并进行了后续调查,询问他们是否有可能将小部件脚本开源。这样做可以使任何人都可以删除支持广泛支持的功能的polyfill,以及发布未转换为ES3的现代代码。综合起来,这些努力将在目前的95k上节省大量的资金,但是不幸的是,我没有收到任何一封电子邮件的回复。

答案 1 :(得分:0)

您可以使用以下网址从Twitter嵌入时间轴:https://publish.twitter.com/

输入来自Twitter的个人资料网址。

它将生成类似于以下代码:

<a class="twitter-timeline" 
  href="https://twitter.com/elonmusk?ref_src=twsrc%5Etfw">
    Tweets by elonmusk
</a> 
<script async 
  src="https://platform.twitter.com/widgets.js" 
  charset="utf-8"></script>

如果将以上代码复制并粘贴到html中,它将嵌入Elon Musk的Twitter时间轴。

答案 2 :(得分:0)

您可以复制并粘贴以下代码,以了解twitter API工具如何显示时间线帖子,主题标签数据,以及查看该帖子中是否提到了该用户。

                <?php include("header.php"); ?>
            <?php
            $user_input=$_GET['user_input'];
            $user_count=$_GET['user_count'];
            $user_until=$_GET['user_until'];
            $mention_count=$_GET['mention_count'];
            $hash_count=$_GET['hash_count'];


            $user_until = date("Y-m-d", strtotime($user_until));



            $user_data=userinfo(
            $user_input,
            $oauth_access_token,
            $oauth_access_token_secret,
            $consumer_key,
            $consumer_secret
            );

            $user_tweets=user_tweets(
            $user_input,
            $oauth_access_token,
            $oauth_access_token_secret,
            $consumer_key,
            $consumer_secret,
            $user_count);



            $mentiondata_content=mentioneddata($oauth_access_token,
            $oauth_access_token_secret,
            $consumer_key,
            $consumer_secret,
            $user_input,
            $user_until,
            $mention_count
            );
            $hashtagdata_content=hashtagdata_content($oauth_access_token,
            $oauth_access_token_secret,
            $consumer_key,
            $consumer_secret,
            $user_input,
            $user_until,
            $hash_count);




            $myJSON = json_encode($user_data);
            $myJSON2 = json_encode($user_tweets);
            $myJSON3 = json_encode($mentiondata_content);
            $myJSON4 = json_encode($hashtagdata_content);


            $users = array();
            $users[]=json_decode($myJSON, true);
            $users[]=json_decode($myJSON2, true);
            $users[]=json_decode($myJSON3, true);
            $users[]=json_decode($myJSON4, true);
            $myJSONdata = json_encode($users);
            $wikidata=wikipedia($user_data['name']);
            if ($user_data==NULL) {
            ?>
            <style type="text/css">
            .user_profile_sec , .profile_data_sec
            {display: none;}
            </style>
            <?php
            }
            ?>

            <section class="user_profile_sec"  >
            <div class="row">
            <div class="col-sm-12">
            Your search results of <?php echo $user_input;?> are given below.
            </div>
            </div>
            <div class="row">
            <div class="col-sm-4">
            <img src="<?php echo $user_data['profile_photo'];?>" style="width: 250px;">
            </div>
            <div class="col-sm-8">
            <div class="w3-padding w3-white user_data_sec01">
            <h4><b><?php echo $user_data['name']." </b>| " . $user_data['screen_name'];?> (<?php echo $user_data['user_id']; ?>)
            <?php
            if ($user_data['verified']==1) {
            echo "<span class='fa fa-check verfication_col'></span>";
            }
            ?>
            </h4>
            Followers =<?php echo $user_data['followers_count'];?> /
            Tweets <?php echo $user_data['statuses_count'];?> /
            friends_count =<?php echo $user_data['friends_count'];?> /
            listed_count =<?php echo $user_data['listed_count'];?> /
            favourites_count =<?php echo $user_data['favourites_count'];?>
            <br>
            <?php   echo $wikidata; ?>
            User Description :  <?php echo $user_data['description'];?>  <br>
            User Location : <?php echo $user_data['location'];?><br>
            User url : <a href="<?php echo $user_data['url'];?>" target="_blank">
            <?php echo $user_data['url'];?></a>  <br>
            Created_at : <?php echo $user_data['created_at'];?> <br>
            <div class="btn-group btn-group-justified">
            <div class="btn-group">
            <button type="button" class="btn btn-primary save_userdata_btn" value="<?php echo base64_encode($myJSONdata);?>"><b> Save Data
            <span class="fa fa-save"></span> </b>
            </button>
            </div>
            <div class="btn-group">
            <button type="button" class="btn btn-primary  "><b> Download Data
            <span class="fa fa-cloud-download"></span> </b>
            </button>
            </div>
            <div class="btn-group">
            <button type="button" class="btn btn-primary"><b> Clear Data
            <span class="fa fa-trash"></span> </b>
            </button>
            </div>
            </div>
            </div>
            </div>
            </div>
            <br>
            </section>


            <section class="profile_data_sec">
            <div class="row">
            <div class="col-sm-3"> <button class="btn btn-primary showbtn1 btn-block">Show default analysis</button>
            </div>
            <div class="col-sm-3">
            <button class="btn btn-primary showbtn1  btn-block">coming soons</button>
            </div>
            <div class="col-sm-3">
            <button class="btn btn-primary showbtn1  btn-block">coming soons</button>
            </div>
            <div class="col-sm-3">
            <button class="btn btn-primary showbtn1  btn-block">coming soons</button>
            </div>
            </div>


            <div class="row">
            <div class="col-sm-12">




            <div class="w3-card w3-white w3-padding">
            <div>
            <div class="w3-right">
            <button type="button" class="btn btn-primary download_data_btn1"><b> Download Data
            <span class="fa fa-cloud-download"></span> </b>
            </button>
            </div>
            <div class="w3-left">
            Result based on Profile.
            </div>
            </div>
            <div class=" ">
            <div class="table-responsivep">
            <table class="table" id="user_tweets_table1">
            <thead>
            <tr>
            <th>No</th>
            <th>Date</th>
            <th>Tweet</th>
            <th>Link</th>
            <th>Retweet</th>
            <th>Likes </th>
            <th>Comments</th>
            <th>Username</th>
            <th>Mentioned</th>
            <th>Hashtags</th>
            </tr>
            </thead>
            <tbody>
            <?php

            $n=1;
            foreach ($user_tweets as $data) {
            $retweeted = strpos($data['full_text'], "RT");
            ?>
            <tr>
            <td><?php echo $n;?></td>
            <td class="datetb"><?php echo date("d-m-Y", strtotime($data['created_at'])); ?>
            </td>
            <td class="tweet_text">
            <?php
            if ($retweeted === false)
            {
            echo $data['full_text'];
            //$sentidata=sentimental_result($data['full_text']);
            }else{
            $rdata=get_tweets($data['id'],
            $oauth_access_token,
            $oauth_access_token_secret,
            $consumer_key,
            $consumer_secret);
            echo "<span class='badge'><i class='fa fa-copy w3-border'></i> </span>" .$rdata['retweeted_status']['full_text'];
            $sentidata=sentimental_result($data['full_text']);
            }
            ?>
            <div class="sentidiv">
            <br>
            <?php //echo sentimental_html($sentidata); ?>
            </div>
            </td>
            <td>
            <?php
            if ($retweeted === false) {
            $user_for_likes= $data['user']['screen_name'];
            $user_id_for_likes= $data['id'];
            }else{
            $user_for_likes= $data['retweeted_status']['user']['screen_name'];
            $user_id_for_likes= $data['retweeted_status']['id'];
            }
            $url="https://twitter.com/".$user_for_likes."/status/".$user_id_for_likes."/";
            ?>
            <button class="btn btn-success btn_view" value="<?php echo $url;?>">
            View
            <span class="fa fa-eye"></span>
            </button>

            </td>
            <td class="nolik retweet_tab">
            <?php
            if ($retweeted === false) {
            $user_for_likes= $data['user']['screen_name'];
            $user_id_for_likes= $data['id'];
            }else{
            $user_for_likes= $data['retweeted_status']['user']['screen_name'];
            $user_id_for_likes= $data['retweeted_status']['id'];
            }
            $status_array=get_status($user_id_for_likes,$user_for_likes);
            echo $status_array['retweets'];
            ?>
            </td>
            <td class="nodot likes_tab">
            <?php
            if ($retweeted === false) {
            $user_for_likes= $data['user']['screen_name'];
            $user_id_for_likes= $data['id'];
            }else{
            $user_for_likes= $data['retweeted_status']['user']['screen_name'];
            $user_id_for_likes= $data['retweeted_status']['id'];
            }
            $status_array=get_status($user_id_for_likes,$user_for_likes);
            echo $status_array['likes'];
            ?>
            </td>
            <td class="comments_tab">
            <?php
            if ($retweeted === false) {
            $user_for_likes= $data['user']['screen_name'];
            $user_id_for_likes= $data['id'];
            }else{
            $user_for_likes= $data['retweeted_status']['user']['screen_name'];
            $user_id_for_likes= $data['retweeted_status']['id'];
            }
            $status_array=get_status($user_id_for_likes,$user_for_likes);
            echo $status_array['comments'];
            ?>
            </td>
            <td>
            <?php
            if ($retweeted === false) {
            echo $data['user']['screen_name'];
            }else{
            echo $data['retweeted_status']['user']['screen_name'];
            }
            ?>
            </td>
            <td class="alignleft">
            <?php
            foreach ($data['entities']['user_mentions'] as $key ) {
            echo "@".$key['screen_name']."<br>";
            }
            ?>
            </td>
            <td class="alignleft">
            <?php
            foreach ($data['entities']['hashtags'] as $key ) {
            echo "#".$key['text']."<br>";
            }
            ?>
            </td>
            </tr>
            <?php
            $n++;
            }
            ?>
            </tbody>
            </table>
            </div>
            </div>
            </div>
            </div>
            </div>
            <br>

            </section>
            <!--   ======================================================================  -->

            <div class="row">
            <div class="col-sm-12">
            <div class="w3-card w3-white w3-padding">
            <div>
            <div class="w3-right">
            <button type="button" class="btn btn-primary download_data_btn2"><b> Download Data
            <span class="fa fa-cloud-download"></span> </b>
            </button>
            </div>
            <div class="w3-left">
            Result based on Mentioning.
            </div>
            </div>
            <div class=" ">
            <div class="table-responsivep">
            <table class="table " id="user_tweets_table2">
            <thead>
            <tr>
            <th>No</th>
            <th>Date</th>
            <th>Tweet</th>
            <th>Link</th>
            <th>No.Retweet</th>
            <th>No.Likes</th>
            <th>No.Comments</th>
            <th>Username</th>
            <th>Mentioned</th>
            <th>Hashtags</th>
            </tr>
            </thead>
            <tbody class="w3-center">
            <?php
            //print_r($mentiondata_content['statuses']);
            $n=1;
            foreach ($mentiondata_content['statuses'] as $data) {
            $retweeted_hash=strpos($data['text'], "RT");
            ?>
            <tr>
            <td># <?php echo $n; ?>
            </td>
            <td class="datetb">

            <?php echo date("d-m-Y", strtotime($data['created_at'])); ?>
            </td>
            <td class="tweet_text">
            <?php
            if ($retweeted_hash === false)
            {
            $rdatas=get_tweets($data['id'],
            $oauth_access_token,
            $oauth_access_token_secret,
            $consumer_key,
            $consumer_secret);
            echo  $rdatas['full_text'];
            }else{
            $rdatad=get_tweets($data['id'],
            $oauth_access_token,
            $oauth_access_token_secret,
            $consumer_key,
            $consumer_secret);
            echo "<span class='badge'><i class='fa fa-copy w3-border'></i> </span>" .$rdatad['retweeted_status']['full_text']."</i></span>";
            }
            ?>
            </td>
            <td>
            <?php
            if ($retweeted_hash === false) {
            $user_for_likes= $data['user']['screen_name'];
            $user_id_for_likes= $data['id'];
            }else{
            $user_for_likes= $data['retweeted_status']['user']['screen_name'];
            $user_id_for_likes= $data['retweeted_status']['id'];
            }
            $url="https://twitter.com/".$user_for_likes."/status/".$user_id_for_likes."/";
            ?>
            <button class="btn btn-success btn_view" value="<?php echo $url;?>">
            View
            <span class="fa fa-eye"></span>
            </button>

            </td>
            <td class="nolik retweet_tab">
            <?php
            if ($retweeted_hash === false) {
            $user_for_likes= $data['user']['screen_name'];
            $user_id_for_likes= $data['id'];
            }else{
            $user_for_likes= $data['retweeted_status']['user']['screen_name'];
            $user_id_for_likes= $data['retweeted_status']['id'];
            }
            //echo get_retweets($user_id_for_likes,$user_for_likes);
            $status_array=get_status($user_id_for_likes,$user_for_likes);
            echo $status_array['retweets'];
            ?>
            </td>
            <td class="nodot likes_tab">
            <?php
            if ($retweeted_hash === false) {
            $user_for_likes= $data['user']['screen_name'];
            $user_id_for_likes= $data['id'];
            }else{
            $user_for_likes= $data['retweeted_status']['user']['screen_name'];
            $user_id_for_likes= $data['retweeted_status']['id'];
            }
            //echo get_likes($user_id_for_likes,$user_for_likes);
            $status_array=get_status($user_id_for_likes,$user_for_likes);
            echo $status_array['likes'];
            ?>
            </td>
            <td class="comments_tab">
            <?php
            if ($retweeted_hash === false) {
            $user_for_likes= $data['user']['screen_name'];
            $user_id_for_likes= $data['id'];
            }else{
            $user_for_likes= $data['retweeted_status']['user']['screen_name'];
            $user_id_for_likes= $data['retweeted_status']['id'];
            }
            //echo get_comments($user_id_for_likes,$user_for_likes);
            $status_array=get_status($user_id_for_likes,$user_for_likes);
            echo $status_array['comments'];
            ?>
            </td>
            <td>
            <?php echo $data['user']['screen_name']; ?>
            </td>
            <td class="alignleft">
            <?php

            foreach ($data['entities']['user_mentions'] as $key ) {
            echo "@".$key['screen_name']."<br>";
            }
            ?>
            </td>
            <td class="alignleft">
            <?php
            foreach ($data['entities']['hashtags'] as $key ) {
            echo "#".$key['text']."<br>";
            }
            ?>
            </td>
            </tr>
            <?php
            $n++;
            }
            ?>
            </tbody>
            </table>
            </div>
            </div>
            </div>
            </div>
            </div>







            <br>
            <!--   ======================================================================  -->

            <div class="row">
            <div class="col-sm-12">
            <div class="w3-card w3-white w3-padding">

            <div>
            <div class="w3-right">
            <button type="button" class="btn btn-primary download_data_btn3"><b> Download Data
            <span class="fa fa-cloud-download"></span> </b>
            </button>
            </div>
            <div class="w3-left">
            Result based on Hashtag.
            </div>
            </div>
            <div class=" ">
            <div class="table-responsivep">
            <table class="table " id="user_tweets_table3">
            <thead>
            <tr>
            <th>No</th>
            <th>Date</th>
            <th>Tweet</th>
            <th>Link</th>
            <th>No.Retweet</th>
            <th>No.Likes</th>
            <th>No.Comments</th>
            <th>Username</th>
            <th>Mentioned</th>
            <th>Hashtags</th>
            </tr>
            </thead>
            <tbody class="w3-center">
            <?php
            //print_r($mentiondata_content['statuses']);
            $n=1;
            foreach ($hashtagdata_content['statuses'] as $data) {
            $retweeted_hash=strpos($data['text'], "RT");
            ?>
            <tr>
            <td># <?php echo $n; ?>
            </td>
            <td class="datetb">

            <?php echo date("d-m-Y", strtotime($data['created_at'])); ?>
            </td>
            <td class="tweet_text">
            <?php
            if ($retweeted_hash === false)
            {
            $rdatas=get_tweets($data['id'],
            $oauth_access_token,
            $oauth_access_token_secret,
            $consumer_key,
            $consumer_secret);
            echo  $rdatas['full_text'];
            }else{
            $rdatad=get_tweets($data['id'],
            $oauth_access_token,
            $oauth_access_token_secret,
            $consumer_key,
            $consumer_secret);
            echo "<span class='badge'><i class='fa fa-copy w3-border'></i> </span>" .$rdatad['retweeted_status']['full_text']."</i></span>";
            }
            ?>
            </td>
            <td>
            <?php
            if ($retweeted_hash === false) {
            $user_for_likes= $data['user']['screen_name'];
            $user_id_for_likes= $data['id'];
            }else{
            $user_for_likes= $data['retweeted_status']['user']['screen_name'];
            $user_id_for_likes= $data['retweeted_status']['id'];
            }
            $url="https://twitter.com/".$user_for_likes."/status/".$user_id_for_likes."/";
            ?>
            <button class="btn btn-success btn_view" value="<?php echo $url;?>">
            View
            <span class="fa fa-eye"></span>
            </button>
            </td>
            <td class="nolik retweet_tab">
            <?php
            if ($retweeted_hash === false) {
            $user_for_likes= $data['user']['screen_name'];
            $user_id_for_likes= $data['id'];
            }else{
            $user_for_likes= $data['retweeted_status']['user']['screen_name'];
            $user_id_for_likes= $data['retweeted_status']['id'];
            }
            //echo get_retweets($user_id_for_likes,$user_for_likes);
            $status_array=get_status($user_id_for_likes,$user_for_likes);
            echo $status_array['retweets'];
            ?>
            </td>
            <td class="nodot likes_tab">
            <?php
            if ($retweeted_hash === false) {
            $user_for_likes= $data['user']['screen_name'];
            $user_id_for_likes= $data['id'];
            }else{
            $user_for_likes= $data['retweeted_status']['user']['screen_name'];
            $user_id_for_likes= $data['retweeted_status']['id'];
            }
            //echo get_likes($user_id_for_likes,$user_for_likes);
            $status_array=get_status($user_id_for_likes,$user_for_likes);
            echo $status_array['likes'];
            ?>
            </td>
            <td class="comments_tab">
            <?php
            if ($retweeted_hash === false) {
            $user_for_likes= $data['user']['screen_name'];
            $user_id_for_likes= $data['id'];
            }else{
            $user_for_likes= $data['retweeted_status']['user']['screen_name'];
            $user_id_for_likes= $data['retweeted_status']['id'];
            }
            //echo get_comments($user_id_for_likes,$user_for_likes);
            $status_array=get_status($user_id_for_likes,$user_for_likes);
            echo $status_array['comments'];
            ?>
            </td>
            <td>
            <?php echo $data['user']['screen_name']; ?>
            </td>
            <td class="alignleft">
            <?php

            foreach ($data['entities']['user_mentions'] as $key ) {
            echo "@".$key['screen_name']."<br>";
            }
            ?>
            </td>
            <td class="alignleft">
            <?php
            foreach ($data['entities']['hashtags'] as $key ) {
            echo "#".$key['text']."<br>";
            }
            ?>
            </td>
            </tr>
            <?php
            $n++;
            }
            ?>
            </tbody>
            </table>
            </div>
            </div>
            </div>
            </div>
            </div>

            <!-- ###########################################################################  -->
            <script type="text/javascript">
            $('.btn_view').click(function(){
            var url=$(this).val();
            var urlis="https://twitframe.com/show?url="+url;
            $('.statuses_body_frame').prop('src', urlis);
            $('#myModal').modal('toggle');
            });


            $('.save_userdata_btn').click(function(){
            var userdata=$(this).val();
            $.ajax({
            type: 'POST',
            url: 'save_user_data.php',
            data: { userdata: userdata },
            success: function(response) {
            alert(response);
            }
            });

            });


            function exportTableToExcel(tableID, filename = ''){
            var downloadLink;
            var dataType = 'application/vnd.ms-excel';
            var tableSelect = document.getElementById(tableID);
            var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');

            // Specify file name
            filename = filename?filename+'.xls':'excel_data.xls';

            // Create download link element
            downloadLink = document.createElement("a");

            document.body.appendChild(downloadLink);

            if(navigator.msSaveOrOpenBlob){
            var blob = new Blob(['\ufeff', tableHTML], {
            type: dataType
            });
            navigator.msSaveOrOpenBlob( blob, filename);
            }else{
            // Create a link to the file
            downloadLink.href = 'data:' + dataType + ', ' + tableHTML;

            // Setting the file name
            downloadLink.download = filename;

            //triggering the function
            downloadLink.click();
            }
            }

            $('.download_data_btn1').click(function(){
            exportTableToExcel("user_tweets_table1","user_datas");
            });
            $('.download_data_btn2').click(function(){
            exportTableToExcel("user_tweets_table2","mentioned_datas");
            });
            $('.download_data_btn3').click(function(){
            exportTableToExcel("user_tweets_table3","hashtag_datas");
            });

            </script>
            <div id="myModal" class="modal fade" role="dialog">
            <div class="modal-dialog">

            <div class="modal-content">
            <div class="modal-body">
            <div class="statuses_body">
            <iframe class="statuses_body_frame" border=0 frameborder=0  style="height: 70vh;width: 100%;"
            src=""></iframe>
            </div>
            </div>
            </div>

            </div>
            </div>



            </body>
            </html>

            <?php include("footer.php"); ?>