使用从 Twitter API 收到的 URL 嵌入推文:一些正在返回错误

Embedding tweets using URL received from Twitter API: some are returning errors

我正在使用 TwitteroAuth API。

我正在使用搜索 API 搜索推文:https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html

通过此方法嵌入(正如您将从代码中看到的):https://developer.twitter.com/en/docs/twitter-for-websites/embedded-tweets/guides/embedded-tweet-parameter-reference

这是我的PHP(已经从推特上得到了json对象):

<?php
$tweet_array = json_decode(json_encode($tweets), true);
  // Turn each item into tweet
  foreach ($tweet_array['statuses'] as $tweet ) {
    // Variables
    $tweet_text = $tweet['text'];
    $twitter_username = $tweet['user']['name'];
    $twitter_handle = $tweet['user']['screen_name'];
    $output = "";
    // Blockquote wrapper
    $output .= "<blockquote class='twitter-tweet' data-lang='en'>";
    // Text
    $output .= "<p lang='en' dir='ltr'>$tweet_text</p>";
    // User name and Handle
    $output .= "&mdash; $twitter_username (@$twitter_handle)";
    // Link to tweet
    foreach ($tweet['entities'] as $key) {
      // So don't break search
      if (empty($key)) {
        // Do nothing
      } else {
        // Check for extended_url key
        if (array_key_exists("expanded_url",($key[0]))) {
          // Boolean to confirm retrieval of URL
          $url = true;
          // URL output
          $url_string = $key[0]['expanded_url'];
          $output .= "<a href='$url_string'>$url_string</a>";
        }
      }
    }
    $output .= "</blockquote>";
    // if URL present, output code
    if ($url == true) {
      echo $output;
    }
  }

该代码输出这个,混合了工作和不工作的推文:

正在输出的代码如下所示(工作和不工作示例):

工作中!

<twitterwidget class="twitter-tweet twitter-tweet-rendered" id="twitter-widget-1" style="position: static; visibility: visible; display: block; transform: rotate(0deg); max-width: 100%; width: 500px; min-width: 220px; margin-top: 10px; margin-bottom: 10px;" data-tweet-id="1057283419007143936"></twitterwidget>

不工作!

<blockquote class="twitter-tweet twitter-tweet-error" data-lang="en" data-twitter-extracted-i1540936951520207597="true"><p lang="en" dir="ltr">He’ll say anything before the election. Don’t take the bait. Focus on ending the hate. Hug a kid. Be nice to someon… <!-- SHORTENED LINK TAKEN OUT FOR STACK OVERFLOW --></p>— Amy Klobuchar (@amyklobuchar)<a href="https://twitter.com/i/web/status/1057234049587167232">https://twitter.com/i/web/status/1057234049587167232</a></blockquote>

任何帮助将不胜感激

我找到了答案。我没有使用模糊的 blockquote 转换方法,而是使用 PHP 为每个具有唯一 twitter ID 的容器打印 JS 脚本。 100% 成功率:

   <?php  /* OUTPUT */
  // Count tweets for debug
  $number_tweets = count($tweet_array['statuses']);
  echo "<div class='cols'>";
  // Loop through each tweet
  foreach ($tweet_array['statuses'] as $tweet ) {
    // Get tweet ID
    $id = $tweet["id"];
    // Create grid item to be targeted by Twitter's widgets.js
    echo "<div class='grid-item'><div id='container-$id'></div></div>";
    // Add to array for JS objet
    $js_array[] = "twttr.widgets.createTweet('$id', document.getElementById('container-$id'));";
  }
  echo "</div>";
  // Begin Javascript
  echo '<script>';
  // Print out JS to convert items to Tweets
  $t = 1;
  foreach ($js_array as $js ) {
    echo $js;

    $t++;
  }
 echo '</script>';