开发者

Tweeting privately via PHP

I have been looking at the twitter开发者_StackOverflow api. I'm kind of lost though. I want to tweet from PHP using a simple web form (1 input, 1 button), this is a private system for my LAN for me and the wife who use the same account. Do I still need to go through the whole OAuth process even though I know I want to tweet already?

This is purely for tweeting, I dont want to return anything except maybe a true or false as to whether it worked.


Twitter provides a process for getting an access token for the account that created the application. If you log into https://dev.twitter.com/apps with the account you wish to tweet from and click the name of the app you wish to use (create one first if you don't already have one). You should know be on the application details page (.com/apps/123) not on the application settings page (.com/apps/edit/123). On the right is a My Access Token option which will give you an access token and an access token secret. These will act similar to a password for the account.

Download the latest version of TwitterOAuth (currently 0.2.0-beta3) and place the twitteroauth.php and OAuth.php files in a directory. Place the following code in a php file in the same directory replacing the four string values with the corresponding values from your app and your access token.

<?php

require_once('twitteroauth.php');
$connection = new TwitterOAuth('app consumer key', 'app consumer secret', 'my access token', 'my access token secret');
$status = $connection->post('statuses/update', array('status' => 'text to be tweeted'));

You can now submit a form to the php file, pass the text to the post() function and it will post a new tweet.


You have to go trough it at least once, but as the authentication doesn't expire at the moment, you can do the "dance" once, and then hardcode the various codes in your app. You do need to do it at least once (register your app, allow your app to use your account, get the code, etc), otherwise there's no telling you know the password of course.


Since you are using a singular account, you will only need to perform the OAuth authentication once. I use the API to tweet for my employers (pittnews.com) and they have about 14 people tweet from the same account using the same OAuth authentication. I store the values in the PHP I utilize for tweeting.


you must set one account by default and use this account... ( save it in conf file and include in php file and if your account expired use below class for new session )

see this class

  • http://classes.verkoyen.eu/twitter

also you can see api manual

  • http://dev.twitter.com/pages/libraries#php


Yes, you need to ultimately obtain an OAuth access token secret for your app to access your Twitter account before you can start tweeting. Since you're using PHP, I'd suggest using the Twitter support within the Zend framework. It can help you perform the OAuth shuffle, and also perform the general Twitter functions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜