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

时间:2018-10-30 22:08:50

标签: php api twitter twitter-oauth

我正在使用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(已经从twitter获得了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;
    }
  }

该代码输出此消息,混合了工作和不工作的推文:

enter image description here

正在输出的代码如下所示(有效和无效的示例):

工作!

<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>

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我找到了答案。我没有使用模糊的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>';