Embed Twitter feed AND REPLIES TO ME in a web page - twitter

I'm using the Twitter-generated "Twitter Badge" HTML and JavaScript to show my Twitter feed on a web page. I'd like to include the replies to me in the same feed, or at least in another one. (My replies to others are already in there.) One the Twitter site, I just click on the #username link on the right panel and get the #replies for my userid. Does anybody know how to embed that in a page? Thanks.

There's an API method that will let you pull the 20 most recent #replies in XML or JSON format; however, it requires authentication, so you'll want to do this on the backend. (There are OAuth libraries for Javascript, but that doesn't sound especially workable to me for this use case.)

The reply link suggested by #Meredith is removed from twitter. Anyone else have a better solution ?

As long as you use twitter regularly enough to have tweets in search(expires after sometime), you could use twitter searches jsonp.
I wrote a simple js lib around it. http://gist.github.com/110884
That will give you an array of matching tweets that you can then style or whatever.
//done up in no framework js
Twitter.search({q:"alan",
callback:function(results){
var body = document.getElementsByTagName("body")[0];
for(var i=0;i<results.length;i++)
{
alert(results[i].text)
}
}
})

Have you considered using Juitter - jQuery Plugin for Twitter?

An easy way to embed any content from an external page (including tweets) is to use the following code:
<?php
echo "<html><head>";
echo "<style> body { background: #FBFAF9; font-family: sans-serif; }";
echo "h4 {text-transform:capitalize; font-family: sans-serif;} h5 {text-transform:capitalize; font-family: sans-serif; color: grey; } </style>";
echo "</head><body>";
$data = file_get_contents('https://twitter.com/your_feed_here');
$data = explode("<div class=\"stream profile-stream\">", $data);
$data = explode("<div class=\"grid hidden\">", $data[1]);
$data = str_replace("</p>", "</p>", $data[0]);
$data = explode("\n", $data);
foreach ($data as $line) {
$date = explode("data-long-form=\"true\">", $line);
$date = explode("</span>", $date[1]);
$article['date'] = $date[0];
$title = explode("<p class=\"js-tweet-text tweet-text\">", $line);
$title = explode("</p>", $title[1]);
$article['title'] = $title[0];
$articles[] = $article;
}
unset($articles[0]);
unset($articles[count($articles)]);
foreach ($articles as $markup) :
?>
<div class="entry">
<h5><?=$markup['date'];?></h5>
<div style="width:270px"><?=$markup['title']?></div>
</div>
<?php endforeach; ?>

Related

YQL: html table is no longer supported

I use YQL to get some html-pages for reading information out of it.
Since today I get the return message "html table is no longer supported. See https://policies.yahoo.com/us/en/yahoo/terms/product-atos/yql/index.htm for YQL Terms of Use"
Example in the console: https://developer.yahoo.com/yql/console/#h=select+*+from+html+where+url%3D%22http%3A%2F%2Fwww.google.de%22
Did Yahoo stop this service? Does anybody know a kind of announcement from Yahoo? I am wondering whether this is simply a bug or whether they really stopped this service...
All documentation is still there (html scraping):
https://developer.yahoo.com/yql/guide/yql-select-xpath.html ,
https://developer.yahoo.com/yql/
A while ago I posted in an YQL forum from Yahoo, now this one does not exist anymore (or at least I do not find it). How can you contact Yahoo to find out whether this service really stopped?
Best regards,
hebr3
It looks like Yahoo did indeed end their support of the html library as of 6/8/2017 (according to my error logs). There doesn't appear to be any official announcement of it yet.
Luckily, there is a YQL community library that can be used in place of the official html library with few changes to your codebase. See the htmlstring table in the YQL Console.
Change your YQL query to reference htmltable instead of html and include the community environment in your REST query. For example:
/*/ Old code /*/
var site = "http://www.test.com/foo.html";
var yql = "select * from html where url='" + site + "' AND xpath='//div'";
var resturl = "https://query.yahooapis.com/v1/public/yql?q="
+ encodeURIComponent(yql) + "&format=json";
/*/ New code /*/
var site = "http://www.test.com/foo.html";
var yql = "select * from htmlstring where url='" + site + "' AND xpath='//div'";
var resturl = "https://query.yahooapis.com/v1/public/yql?q="
+ encodeURIComponent(yql) + "&format=json"
+ "&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
Thank you very much for your code.
It helped me to create my own script to read those pages which I need. I never programmed PHP before, but with your code and the wisdom of the internet I could change your script to my needs.
PHP
<?
header('Access-Control-Allow-Origin: *'); //all
$url = $_GET['url'];
if (substr($url,0,25) != "https://www.xxxx.yy") {
echo "Only https://www.xxxx.yy allowed!";
return;
}
$xpathQuery = $_GET['xpath'];
//need more hard check for security, I made only basic
function check($target_url){
$check = curl_init();
//curl_setopt( $check, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"));
//curl_setopt($check, CURLOPT_INTERFACE, "xxx.xxx.xxx.xxx");
curl_setopt($check, CURLOPT_COOKIEJAR, 'cookiemon.txt');
curl_setopt($check, CURLOPT_COOKIEFILE, 'cookiemon.txt');
curl_setopt($check, CURLOPT_TIMEOUT, 40000);
curl_setopt($check, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($check, CURLOPT_URL, $target_url);
curl_setopt($check, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($check, CURLOPT_FOLLOWLOCATION, false);
$tmp = curl_exec ($check);
curl_close ($check);
return $tmp;
}
// get html
$html = check($url);
$dom = new DOMDocument();
#$dom->loadHTML($html);
// apply xpath filter
$xpath = new DOMXPath($dom);
$elements = $xpath->query($xpathQuery);
$temp_dom = new DOMDocument();
foreach($elements as $n) $temp_dom->appendChild($temp_dom->importNode($n,true));
$renderedHtml = $temp_dom->saveHTML();
// return html in json response
// json structure:
// {html: "xxxx"}
$post_data = array(
'html' => $renderedHtml
);
echo json_encode($post_data);
?>
Javascript
$.ajax({
url: "url of service",
dataType: "json",
data: { url: url,
xpath: "//*"
},
type: 'GET',
success: function() {
},
error: function(data) {
}
});
Even though YQL does not support the html table anymore, I've come to realize that instead of making one network call and parsing out the results it's possible to make several calls. For example, my call before would look like this:
select html from rss where url="http://w1.weather.gov/xml/current_obs/KFLL.rss"
Which should give me the information as such below
Now I'd have to use these two:
select title from rss where url="http://w1.weather.gov/xml/current_obs/KFLL.rss"
select description from rss where url="http://w1.weather.gov/xml/current_obs/KFLL.rss"
.. to get what I want. I don't know why they would deprecate something like this without a fallback clearly listed but you should be able to get your data this way.
I build an open source tool called CloudQuery (source code)provide similar functionality as yql recently. It is able to turn most websites to API with some clicks.

Get twilio recording for each call record

It looks like all the guides are outdated on this matter, and twilio's website doesnt have a clear answer for this.
Im trying to get a list of all calls, and for each call record check for a recording record, if it has a recording record then get the uri for it.
Although I dont think this is the correct way of doing what im trying to do the script is very very slow, and doesnt work as expected , here is where im at right now :
// Set our AccountSid and AuthToken
$sid = 'MY_SID';
$token = 'MY_TOKEN';
// Your Account Sid and Auth Token from twilio.com/user/account
$client = new Client($sid, $token );
// Loop over the list of calls and echo a property for each one
foreach ($client->account->calls->read() as $call
) {
echo $call->sid.", ".getRecording($call->sid)."<br/>";
}
function getRecording($callsid){
// Set our AccountSid and AuthToken
$sid = 'MY_SID';
$token = 'MY_TOKEN';
$client = new Client($sid, $token);
// Loop over the list of recordings and echo a property for each one
foreach ($client->account->recordings->read( array( "CallSid" => $callsid )) as $recording ) {
return " ->".$callsid." <a href='http://api.twilio.com".$recording->uri."'>Audio</a> ";
}
}
The output is that all the recording URI are the same for each .
CAb5323eed7ed4f82b3990830777c02684, ->CAb5323eed7ed4f82b3990830777c02684 <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CA57df3525265949c4dfcaa9073b02880a, ->CA57df3525265949c4dfcaa9073b02880a <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CA31f0ac07483d72a56d424b55672a61ab, ->CA31f0ac07483d72a56d424b55672a61ab <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CAac6e6f0d45cd15069300202ce6cbc27e, ->CAac6e6f0d45cd15069300202ce6cbc27e <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CAe51db5d605b94c7141d43611bc8dbbd1, ->CAe51db5d605b94c7141d43611bc8dbbd1 <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CAbe46fe9ab0202fc15184915b0af94d1a, ->CAbe46fe9ab0202fc15184915b0af94d1a <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CA15c3eaccc8b1cfca648105744c1c1c8c, ->CA15c3eaccc8b1cfca648105744c1c1c8c <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CAcb9a5d1f7e3f3b4f3b1eff08f4e51094, ->CAcb9a5d1f7e3f3b4f3b1eff08f4e51094 <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CAfc6a986c4e58e35778d4242303f37e32, ->CAfc6a986c4e58e35778d4242303f37e32 <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CA58aa5dc00c72567b91b43db52577080a, ->CA58aa5dc00c72567b91b43db52577080a <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
CA65dbdee33266a706f17616ecf03e78eb, ->CA65dbdee33266a706f17616ecf03e78eb <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a>
Im looking for a better solution, because this doesnt work and this is also running very very slowly.
Twilio developer evangelist here.
You're right looping over all calls and then looping over all recordings of the call via a REST API is going to be very slow. I recommend that you do not take this approach in order to display the recordings.
Instead, there are two things that you can do.
Firstly, write a script similar to what you already have, but instead of writing out HTML, save the calls and their recordings to a database. That way, you can look through your own database which will be much quicker than making multiple calls to an API.
Secondly, rather than keep running that script to update for new calls, you can use recordingStatusCallbacks on calls. This allows you to set a webhook URL so that when a new call's recording is complete, your application will receive an HTTP request with all the information on the recordings. Then you can save that to your database too and your application will be kept updated with all the latest recordings.
Let me know if that helps.
This is actually a very quick and a great way.
Solution provided by a Twilio employee. Thanks
$client = new Client($sid, $token);
// Create an array of recordings
$recording_array = array();
// Loop over the list of recordings and echo a property for each one
foreach ($client->recordings->read() as $recording) {
$recording_array[$recording->callSid][$count] = $recording->sid;
$count++;
}
foreach ($client->account->calls->read() as $call) {
// Check if there is a call sid exist
if(array_key_exists($call->sid, $recording_array)){
foreach($recording_array["$call->sid"] as $key=>$val){
echo $call->sid.", Recording is ".$val."\r\n";
}
} else {
echo $call->sid."\r\n";
}
}

Retrieve YouTube live_stats concurrent viewers from channel instead of specific live event video

I know that it is possible to get the number of concurrent viewers for a specific live streaming YouTube event with this link: https://www.youtube.com/live_stats?v={videoid}
I was wondering if is it possible to get the live_stats for a channel or playlist instead of a specific live event.
I want to embed this data in a webpage that will have multiple different live events occurring weekly. Changing the video id for each event will be a burden. If this can't be done directly, is there a way to get the video id of a current live event from a channel and use java script or php to replace the id in the link? Please help me figure this out.
After some time, I figured this out myself...
I created a PHP script that retrieves the video id of the first video in a playlist and puts the id into the live stats link. I take the link of live events and put them into a playlist for easy use.
<?php
// Retrieves video info from Youtube playlist. Just update [PLAYLIST_ID] and [API_KEY]
$json_url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=1&playlistId=[PLAYLIST_ID]&fields=items%2Fsnippet%2FresourceId%2FvideoId&key=[API_KEY]";
$json = file_get_contents($json_url);
$json=str_replace('},
]',"}
]",$json);
$data = json_decode($json, true);
$videoId = $data['items'][0]['snippet']['resourceId']['videoId'];
$viewers = file_get_contents("https://www.youtube.com/live_stats?v=$videoId");
echo $viewers;
?>
I then created an HTML page where the data is dynamically updated using jQuery.
<html>
<body>
<div id="status" style="color: #666; line-height: 24px; font-size: 19px; font-weight: normal; font: 19px Roboto,arial,sans-serif;"></div>
<script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function update() {
$.ajax({
url: 'viewers.php',
dataType: 'text',
success: function(data) {
if (parseInt(data) == 0) {
$("#status").css({ display: "none" });
} else {
$("#status").text(parseInt(data) + ' watching now' );
}
}
})
}
update();
var statusIntervalId = window.setInterval(update, 5000);
</script>
</body>
</html>
This is how far I got. Now I am just wondering if there is a better way to combine these codes together to create less server requests. Each jQuery request happens every 5 seconds and is approximately 240 bytes is size. Even though the requests are small, they might still slow down a page.
If you can, please help me improve my solution. Thank you in advance.

Using Twitter OAuth to authenticate API calls for trends

I am working on a website that allows the user to search for the top ten twitter trends in a city or country. At first I was only relying on Twitter's Rest API, but I was having a lot of rate limit issues (at school my rate limit disappears faster than I have a chance to use it). I know that authenticating my API calls will help me to better deal with this issue (Authenticated API calls are charged to the authenticating user’s limit while unauthenticated API calls are deducted from the calling IP address’ allotment).
I implemented #abraham's PHP library (https://github.com/abraham/twitteroauth), unfortunately my API calls aren't being authenticated. I know I have implemented #abraham's PHP library, because it prints out my user information at the end like it should. I have my twitter trend search underneath it but the API call isn't being authenticated. I am not sure how to fix this, and any help would really be appreciated!
This is what I use to get the top ten trends by country:
function showContent(){
// we're going to point to Yahoo's APIs
$BASE_URL = "https://query.yahooapis.com/v1/public/yql";
// the following code should only run if we've submitted a form
if(isset($_REQUEST['location']))
{
// set a variable named "location" to whatever we passed from the form
$location = $_REQUEST['location'];
// Form YQL query and build URI to YQL Web service in two steps:
// first, we show the query
$yql_query = "select woeid from geo.places where text='$location'";
// then we combine the $BASE_URL and query (urlencoded) together
$yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
//var_dump($location);
// show what we're calling
// echo $yql_query_url;
// Make call with cURL (curl pulls webpages - it's very common)
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
// Convert JSON to PHP object
$phpObj = json_decode($json);
// Confirm that results were returned before parsing
if(!is_null($phpObj->query->results)){
// Parse results and extract data to display
foreach($phpObj->query->results as $result){
//var_dump($result);
$woeid = $result[0]->woeid;
if (is_numeric ($location))
{
echo "<span style='color:red; padding-left: 245px;'>Please enter a city or a country</span>";
}
else if(empty($result)){
echo "No results found";
}
else {
/* echo "The woeid of $location is $woeid <br />"; */
}
}
}
$jsontrends=file_get_contents("http://api.twitter.com/1/trends/".$woeid.".json");
$phpObj2 = json_decode($jsontrends, true);
echo "<h3 style='margin-top:20px'>TRENDS: ".$phpObj2[0]['locations'][0]['name']."</h3> \r\n";
$data = $phpObj2[0]['trends'];
foreach ($data as $item) {
echo "<br />".$item['name']."\r\n";
echo "<br /> \r\n";
}
if(empty($item)){
echo "No results found";
}
}
}
I then add it to #abraham's html.inc file (along with some php to see the rate limit status) and html.inc is included in the index.php:
<h1>Top Twitter Trends</h1>
<form name='mainForm' method="get">
<input name='location' id='location' type='text'/><br/>
<button id='lookUpTrends'>Submit</button>
</form>
<?php showContent();
$ratelimit = file_get_contents("http://api.twitter.com/1/account/rate_limit_status.json");
echo $ratelimit;
?>
</div>
#abraham's index.php file has some example calls, and since my call doesn't look like this I think that is probably why it isn't being authenticated.
/* Some example calls */
//$connection->post('statuses/update', array('status' => date(DATE_RFC822)));
//$connection->post('statuses/destroy', array('id' => 5437877770));
//$connection->post('friendships/create', array('id' => 9436992));
//$connection->post('friendships/destroy', array('id' => 9436992));
Please help me find what I need to fix so that my API calls are authenticated.
update 10-21
I think in order to make an authenticated API call I need to include something like this is my code:
$connection->get('trends/place', array('id' => $woeid));
It didn't fix my problem, but maybe it is on the right track?
First off, you'll find that keeping your PHP and HTML separate will really help streamline your code and keep logical concerns separate (aggregating the data and displaying it are two different concerns)(many PHPers like MVC).
The code you have shared appears to be correct. My guess is that the issue lies in the creation of the OAuth connection, which should look something like:
<?php
/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token,$secret);
Where CONSUMER_KEY and CONSUMER_SECRET are from your Trends Test app and $token and $secret are from the user signing in to twitter and allowing your app permission. Are all these values showing up when you create the TwitterOAuth object?
Also, be sure you update the config items in the twitteroauth.php file (specifically line 21 should be set to use the 1.1 API and line 29 should be set to 'json').

Call data from Twitter API

I am trying to write some code in php to show on a website exactly how many times a particular url has been tweeted on Twitter.
I assume I use the info in this page;
http://api.tweetmeme.com/url_info?url=http://dori.co.nz/
But what code would I need to add to the header of a Wordpress theme to call those values?
Many thanks in advance!
<?php
$url = "http://api.tweetmeme.com/url_info?url=http://dori.co.nz/";
$test = file_get_contents($url);
$xml = simplexml_load_string($test);
echo $xml->story->url_count;
?>
Here we go.

Resources