Solved

How can you create a PHP class that sends and pulls tweets from any user

Posted on 2011-09-13
9
378 Views
Last Modified: 2013-12-13
I am currently new to Twitter API and I have to create a PHP class which could be used by an application to send and pull tweets for any given user. I am using twitteroauth library https://github.com/abraham/twitteroauth. But I am not able to test the file and connect to twitter using instructions given in the above link. So could some one guide me so as to how do I use this library to create my PHP class.

Thanks in Advance
0
Comment
Question by:ishani_v
  • 5
  • 4
9 Comments
 
LVL 2

Expert Comment

by:blue-dev
ID: 36529917
What exactly is not working? The library looks pretty straight forward and easy to me.
This is how I got it working in a few minutes:
 - Download the whole package
 - Open the config.php and fill in your consumer key and consumer secret

The library itself is working now, just call the index.php and see for yourself.
If you want to include this library in your own project, simply copy the includes from the index.php, like this for example:

<?php

require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
 
/* Some example calls */
//$connection->get('users/show', array('screen_name' => 'abraham')));
//$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)));

?>

Open in new window


This part does not include allowing your application to do the calls, in prior to this you have to use the connection object to fetch an allowance url. This could look like this:

$url = $connection->getAuthorizeURL($token);
header('Location: ' . $url);

Open in new window


This should get you pretty much up and running as there is no more stuff that needs to be done, the rest is just learning the twitter api and using it and has nothing to do with the library in particular.

You can get information about the twitter api here: https://dev.twitter.com/
0
 

Author Comment

by:ishani_v
ID: 36530125
Hi blu-dev,

Thanks for your reply. I would quickly brief you with what I have done so far.

I have downloaded the twitteroauth PHP library. I have registered my application and got the Consumer Key and Consumer Secret which I have replaced it in the code to get the connection established. I also have my access tokens (oauth_token and oauth_token_secret).

I start running my application with connect.php. After clicking on "Sign in with Tweeter" button, it redirects me to redirect.php where in if I am not already signed in tweeter, I am asked to do so. And if I am signed in Twitter, it asks me whether or not to Authorize my Application. (which I think is right so far).
 At this point of time my address bar contains something like this, https://api.twitter.com/oauth/authorize?oauth_token='some random generated number'.

Once I click on "Authorize" button it does generate a 7-digit Pin which I would have to enter in my application homepage to complete the authorization process.

Thus in this way, I would be able to read tweets from the user who authorized my application.  but what should I do in order to post tweets to these authorized users. Which file should I make the changes and what precisely would be the changes?

Let me know if I have gone wrong somewhere. And do let me know about how to proceed ahead with my query.

Thanks in advance.
0
 
LVL 2

Accepted Solution

by:
blue-dev earned 500 total points
ID: 36530217
Hi,

I can't tell you what to do exactly to post tweets as this has nothing to do with the library you are talking about but with the Twitter Api.

The library allows you to make calls like this:
/* Some example calls */
//$connection->get('users/show', array('screen_name' => 'abraham')));
//$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)));

Open in new window


All you have to do, is to read the api documentation twitter provides, and work out on what parameters and requests you to insert to get a tweet posted. I can image it would be something like:
$connection->post('tweet/new', array('text' => 'some text')));

Open in new window


And again, this is a library.
You have to be able to program php to use it. If you can program in php, don't use it. Either learn php, or hire / pay someone to do the work for you because without knowledge of php you won't get far. The libray includes nothing so far except the simple example you ran of authenticating with twitter (which is really nothing more then an example, you can't simply copy and paste that stuff in your application / website and think that it will work).
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:ishani_v
ID: 36530333
Hi,

Thanks for your reply again. I would certainly read the Twitter API and also more about PHP classes to brushen my skills further.

I do have a fair knowledge about PHP and its coding which is why I was planning to create a PHP class for my application which can send and pull tweets for any given amount of users in my application.

Since its associated with Twitter, I would not have been able to do it without using this Twitter API. The authorization part that I have got working is one of the small bit that I need for my application.

Could you just help me create a basic skeleton for my class PHP which includes this authorization feature allowing the application to read tweets for any user and also the part where I can post tweets for my authorized users.

It would be great if you could throw some light in this area.

Thanks in advance.
0
 
LVL 2

Expert Comment

by:blue-dev
ID: 36530445
Seriously if you have a fair knowledge of php, this should be a piece of cake for you.
There is no additional class necessary as the whole authentication process is shaped in a way that would only be hurtful to implement using classes and not beneficial to any project.

The authentication itself is already provided by the application in a form which is easily usable, namely the twitter-oauth class provided with it.
All thats left is a simple check whether the user is already authenticated and then redirecting him to a webpage where he allows your application to make requests.
This is also already provided by the library, you can see the function calls in redirect.php and connect.php. Bundling those in a class would be pretty stupid as they are neither to low or to high when it comes to abstraction but abstracted the only way possible.
Even if you would combine them in a class, you would just repeat the functions of the original class e.g.:

class yourtwitterclass {
  public function auth(<..data..>) {
    $api->auth(<..data..>);
  }
}

Open in new window


If you are not able, to get this library to tweet a post, then you are far from being able to program php. All the code is already written for you within the library in a form, that is so easy, that one simple look is enough to understand what is happening. If you don't get it, don't bother with is. It is as easy as it can get.

And again, those two pars, authentication and posting, are already in the library. Auth is in connection.php and redirect.php and to post a tweet, go to dev.twitter.com and look up the api key and simply insert it in the libraries post function.
0
 

Author Comment

by:ishani_v
ID: 36530578
Hi

I am sorry I meant that I have a fairly basic knowledge of PHP or else wouldn't be looking for something very obvious as it is. I should apologize for my typing mistake that did mislead you to think that I am very good with PHP.

As for your suggestion, I would go with your opinion not to create any explicit class to authorize the users to my application. As I can see that the library functions already do that. As for posting the tweets to these authorized users I would definitely look up in the Twitter API Documentation.

I just dint get the last part of your reply of adding it in the libraries Post function. If you could elborate that for me. I am sorry If I sound silly in my doubts as I am new to Twitter API structure and its implementation it using PHP.

But I really appreciate your help so far and the solutions that you provide.

Thanks.



0
 
LVL 2

Expert Comment

by:blue-dev
ID: 36531299
Hi,

please just go to dev.twitter.com and look up the api parameters.
The library you are using provides two functions, get and post, namely the two different kinds of http requests.

All you have to do, is go to dev.twitter.com, and look up the api parameters for "posting tweets".
A sample api call could be "twitterpostservice/posttweet", with the parameters "tweettext" containing the tweet. You can use the library to map this to php with just one line:

$connection->post('twitterpostservice/posttweet', array('tweettext' => 'some text')));

Open in new window


Of course I have no idea what the name of the api is, this is just an example. Look it up at dev.twitter.com.
0
 

Author Comment

by:ishani_v
ID: 36531531
Hi,

Thanks for the reply again. I would certainly look up the dev.twitter.com for examples and API parameters for posting the tweets. Thanks for your help through out.


0
 

Author Closing Comment

by:ishani_v
ID: 36549454
It wasn't exactly the solution I was looking for but it did give me a hint on how to proceed further. I finally figured out my solution through it.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP & MySQL - Rounding Results from a Select Query 3 25
Php recording post 4 40
mysql date time 14 30
How can I make this form submit to itself? 10 27
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The viewer will learn how to dynamically set the form action using jQuery.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question