【问题标题】:Twitter API not responding with whole tweet - twitterTwitter API 没有响应整个推文 - twitter
【发布时间】:2020-10-25 19:36:40
【问题描述】:

我正在创建一个 Twitter API,但 API 没有返回整个推文,当它们是推文中的链接时,它似乎停止了。我正在使用 PHP 和 TwitterAPIExchange.php,我该如何解决这个问题?

例子:

Ingewikkelde antwoorden op vragen van @AgnesMulderCDA over de #waakvlam van de #NAM。 De NAM hoeft pas 在 11 月 t ...... https://t.co/ZM0Tuk9AYv

应该是这样的:https://twitter.com/SGaster/status/849334531907887104

$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $access_token, $access_token_secret);

$content = $connection->get("account/verify_credentials");

   $tweets = $connection->get('favorites/list',['count' => 50,'include_entities' => false,'screen_name' => $screen_name]);
    if(isset($tweets))
    {
        displayTweets($tweets);               /* To display follower's tweets (This function is in file functions.php) */
    }

else
{
    echo "<div>";
    echo "Can't fetch Tweets of this user.Please Refresh this page or try again later.";
    echo "</div>";
}

如何扩展推文?

编辑;这是我的 displayTweets() 文件

<?php
/* This file contains one function 'displayTweets'*/

/* displayTweets() function is used to display Tweets in jQuery slider */
require 'lib/makeLinks.php';
function displayTweets($tweets)


{
$i = 0;
foreach ($tweets as $key) 
    {
    if ($i<=200)
    {
        if (isset($key->user->profile_image_url_https))
        {
        $tweet_profile_url = $key->user->profile_image_url_https;
        }
    echo "<div class='tweetBox'>";
    echo "<div class='media slides' style='height:370px;'>";
        echo "<div class='media-left'>";
        if (isset($tweet_profile_url))
        {
        echo "<img src='".$tweet_profile_url."' class='media-object' style='height:50px,width:50px;'/>";
        }
        echo "</div>";
        echo "<div class='media-body'>";
        if (isset($key->user->name))
        {
        echo "<b style='font-size: 25px;'>".$key->user->name."</b>";
        }
        /*if (isset($key->user->verified))
        {
        if ($key->user->verified == 1)echo "<img src='images/download.png' class='verified_twitter_img' style='height:15px;
  width:15px;'/>";
        }*/
        if (isset($key->user->screen_name))
        {
        echo " @".$key->user->screen_name."&diams;"; //will display black diamond between screen_name of user and date of tweet
        }
        if (isset($key->created_at))                                        //date of tweet
        {
    $formatted_date = date_formate($key->created_at);
            echo $formatted_date;
        }
        echo "<br/>";
        if (isset($key->text))
        {
        $tweet_and_links  makeLinks($key->text); //This function(lib/makeLinks.php file) is used to find links in tweet and place it in <a> tag 
        }

            
        if (isset($tweet_and_links))
        {
        echo $tweet_and_links;
        }
        echo "<br/>";
        if (isset($key->entities->media)) {

        foreach ($key->entities->media as $media) {
            echo "<img src='".$media->media_url_https.":small' style='height:280px;width:270px;'/>"; //':small' is written for size, Twitter API provides four sizes - thumb,small,medium and large
        }
            }
            echo "<br/>"; 
        echo "</div>";
    echo "</div>";
    echo "</div>";
    $i++;
    } else
    {
    break;
    }
}
}

function date_formate($dt) {
            //convert date from +0000 timezone to +0530 timezone
            $date = new DateTime($dt);
            $date->setTimezone(new DateTimeZone('Asia/Muscat')); //Srilanka time zone is used because Indian time zone is not provided by twitter
            $changed_date = $date->format('H:i, M d y');
        return $changed_date;
}

【问题讨论】:

    标签: php api twitter


    【解决方案1】:

    这是因为推文最初是 140 个字符。因此 API 响应的 text 字段限制为 140 个字符。如果更长,你会看到... https:// t.co/...

    您需要查看extended_tweet。因此,在您的 PHP 函数 displayTweets() 中,您需要以下内容:

    if ($tweet["truncated"] == true) {
       return $tweet["extended_tweet"]["full_text"];
    }
    

    有一个helpful guide on the official documentation

    【讨论】:

    • 非常感谢您的回答,但它对我没有用 - 如果您可以再看看我刚刚编辑了我的问题 :)
    • 您还需要检索扩展推文。 $tweets = $connection->get('favorites/list',['count' => 50,'include_entities' => false,'screen_name' => $screen_name,'tweet_mode' => 'extended']);
    猜你喜欢
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多