I am using the TwitteroAuth API.
I am searching for Tweets using the search API: https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html
Embedding via this method (as you will see from code): https://developer.twitter.com/en/docs/twitter-for-websites/embedded-tweets/guides/embedded-tweet-parameter-reference
Here is my PHP (having already gotten the json object from twitter):
<?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 .= "— $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;
}
}
That code outputs this, a mix of working and not working tweets:
The code being output looks like this (working and not working examples):
Working!
<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>
Not working!
<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>
Any help would be appreciated immensely
I found an answer. Rather than using the vague blockquote conversion method, I instead used PHP to print a JS script for each container with a unique twitter ID. 100% success rate:
<?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>';