【问题标题】:Embedding tweets using URL received from Twitter API: some are returning errors使用从 Twitter API 接收的 URL 嵌入推文:有些返回错误
【发布时间】:2019-04-04 01:01:36
【问题描述】:

我正在使用 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;
    }
  }

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

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

工作!

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

任何帮助将不胜感激

【问题讨论】:

  • 我没有看到您在 php 中打印“”的位置。
  • 我没有,链接的文档解释说,带有推文链接或 ID 的块引用会被 widgets.js 转换为 twitterwidget。我创建了一个不同的解决方案,请阅读编辑。

标签: php api twitter twitter-oauth


【解决方案1】:

我找到了答案。我没有使用模糊的块引用转换方法,而是使用 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>';

【讨论】:

    猜你喜欢
    • 2011-02-19
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    相关资源
    最近更新 更多