PHP to retrieve latest tweet - twitter

I am currently using the following code to retrieve the latest tweet from my Twitter feed:
<?php
$username='myUsername';
$format = 'json';
$tweet = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}"));
$latestTweet = htmlentities($tweet[0]->text, ENT_QUOTES);
$latestTweet = preg_replace('/http:\/\/([a-z0-9_\.\-\+\&\!\#\~\/\,]+)/i', 'http://$1', $latestTweet);
$latestTweet = preg_replace('/#([a-z0-9_]+)/i', '#$1', $latestTweet);
echo '<p class="inset white">'.$latestTweet.'</p>';
?>
On MAMP, this works perfectly, however on my live site, I get nothing, not even a server side error message. I read on a similar post that this may be something to do with allow_url_fopen = On not being set in php.ini, but setting that appears to have made no difference.
Not withstanding the fact that Twitter may have changed their policy on usage of the API (which I don't believe they have), then I'm lost as to what could be the problem.

I'm using the same approach before, but unfortunately, it's using Twitter API v1 and v1 is no longer available (check https://dev.twitter.com/blog/api-v1-retirement-final-dates for more details). You need to have the OAuth consumer and user access now. GitHub themattharris' tmhOAuth library is handy for displaying Twitter feed.
You may check the question I've posted here at stackoverflow as a reference to your problem:
Display latest Twitter Feed "date created" using tmhOAuth and PHP
It already have the codes how to access the common information needed to be retrieved in Twitter.

Related

Use Tweepy to extract Twitter follower information (API incompatibility issues)?

I was following this tutorial, https://towardsdatascience.com/how-to-download-twitter-friends-or-followers-for-free-b9d5ac23812, which was written in 2021. It should've worked fine, however, they have to 'fix' the things that just work.
Specifically, running this line
for fid in Cursor(api.followers_ids, screen_name=screen_name, count=5000).items():
ids.append(fid)
gives the error:
"tweepy.error.TweepError: [{'message': 'You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve', 'code': 453}]"
I could have pulled the data in five minutes. Now debugging this already cost one hour+ because they just break the things that work. Is there anyway to make this old code snippet work? The application to use API 1.1 takes weeks, and I don't have time to watch their bad documents of how to migrate from API 1.1 to 2.0 and then the documents of migrating from Tweepy 3.9.0 to 4.0.0. Five minutes' task would just become half a day. Thanks in advance for any help.
First of all, have you at least tried to apply for the Elevated access?
It can take some time, it's true, but it can also be instantaneous.
The other solution would be to use the Twitter API V2.
You don't need any tutorial, just read the documentation:
Here for the authentication ;
Here for the retrieval of the followers ;
Here for the pagination.
And you should get something like that:
import tweepy
client = tweepy.Client("Bearer Token here")
paginator = tweepy.Paginator(
client.get_users_followers,
id=..., # ID only, no screename
max_results=1000
).flatten()
for follower in paginator:
print(follower.id)
Finally, even if I understand your frustration (and developing Twitter applications can be very frustrating), I think that you should try to keep it out your SO questions. Good luck!

Unable to retrieve notes with the OneNote API

Aim: Use Rails to retrieve list of notes in a defined section. As seen in lines 249-287 of GetPagesExample.cs
Problem: Receiving 401 when I attempt a GET. Documentation indicates "401 - Unauthorized Problems occurred in authenticating the token, user or your app".
I'm using the OneNoteAPISampleRuby as baseline, to achieve this. The sample comes with examples of how to POST but no examples of how to GET. Posting from the sample app in my local environment works fine. I know that doing a GET on this request URL https://www.onenote.com/api/v1.0/sections/{sectionId}/pages works in the OneNote API Console. This is the URL i'm trying to get to work from my modifications to the sample app.
My modifications are as follows:
<button type="submit" name="submit" value="quotes">Get Quotes</button> at line 10 of index.html.erb
when 'quotes'
result = onenote_client.get_page_in_section(access_token)
at line 29 of welcome_controller.rb
I added a get_page_in_section method to one_note_sharer.rb as seen here: https://gist.github.com/komplexb/347b7dd61ed3338ab6f4
It uses the rest-client gem for the POST examples, so I've been looking at the docs for how to use GET but it appears I've hit a wall.
I noticed someone had a similar problem here, however that answer hasn't been useful for me, since scope office.onenote should give me read access at least according to the documentation.
Looks as if the issue was with how headers and params are combined for GET requests in rest-client library, as seen here. Corrected get_page_in_section method.

How to use Twitter User Streams

I am new in Twitter API. I have a requirement like I have to pull the information of an authenticated twitter user to know the number of followers. I can do this using REST API but I have a problem here. So I have taken the way to use Twitter User Streams.
I am not able to integrate this functionality in the site. I am using twitter console( https://dev.twitter.com/console ). But there I am not able to get any kind of result, it actually hangs/shows gateway timeout. And that's for I am not able to configure that how to write the code for this or how to use this in site. I am not passing any kind of parameters.
So, any body can please help in this would be very grateful( a clean sample code for this or any link where to get the good documentation ). I have already checked with twitter dev documentations.
Thanks in advance :)

Get tweets for a specific user V1.1

I'm not really sure where to begin with this.
Prior to the upgrade to version 1.1 of the API I was able to request tweets in XML format using
$url = 'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=' . $TWITTER_USERNAME;
A couple of things have changed, the return format is now JSON which isn't a problem but I'm not sure how to authenticate my app to request the tweets in JSON format.
I've read this link but am unsure how I'm supposed to implement it.
For instance, where do I get the consumer secret from ?
How do I request a bearer token in PHP ?
Any pointers would be great
you need to use plugin that is written for v1.1, In new version they have added security layer so you need to be authorized in order to get the tweets via API, here are the links that will be helpful for you
http://www.webdevdoor.com/php/authenticating-twitter-feed-timeline-oauth/ http://www.fullondesign.co.uk/coding/2516-how-use-twitter-oauth-1-1-javascriptjquery.htm

How to get results with twitter search api?

I wish to get tweets with a keyword. But There is no result with any keyword.
http://search.twitter.com/search.json?q=summer
How to get results with Twitter Search API?
Version 1 of the twitter API has been deprecated and is being removed. Not sure how you can miss the giant warnings on the twitter dev site ;) This means simple code like the above will not work any more.
So, you now need to make authenticated requests (OAuth) using the 1.1 API, and it's nowhere near as simple as just doing a (in PHP) file_get_contents(http://search.twitter.com/ ...).
I couldn't see any server-side languages you use from your profile, but I wrote a lengthy post explaining the issue (with pictures) and how to use a php library to perform authenticated requests.

Resources