views:

1426

answers:

5

Hello

How to use twitter api with "http basic auth"?

I think I should use the "consumer key"! because twitter gave you limit rate of requests per hour, how can they count my requests if I didn't use my consumer key?

+1  A: 

I would think that they count the requests from the same IP - but I haven't double checked.

Even dynamic IP address will be static for a session and, in the absence of any other identifying information, the only thing that will distinguish you from other users.

As other posters have said - there are better methods if you've got a key.

ChrisF
Why the down vote? I said I wasn't sure, but @ceejayoz has confirmed it.
ChrisF
Thanks, I also think they count the requests from IP, I don't know who down vote your answer however i will give it up vote.thanks
ahmed
I don't mind being down voted - but it would be nice to know why so I can correct my answer where its wrong.
ChrisF
+1  A: 

Authenticated API GET requests are counted against your user account's tally.

Unauthenticated API GET requests (permitted by some methods) are counted against your IP address's tally.

POST requests are not rate limited.

More details are available in the Twitter docs.

ceejayoz
thanks, if it is counted against your user account's tally? when twitter gave my new web application 20000 request? how do they identify if this request comes from this app or that? maybe the consumer key or the domain name!?
ahmed
When you register for whitelisting (to get the 20k req/hr limit), you have to provide an IP address. All requests from that IP address are then counted against the 20k/hr limit, regardless of the user or application that makes them.
Dave Sherohman
A: 

I recently wrote some PHP to post to Twitter This is the working part of it:

$message = 'A new revision (#' . $data['revision'] . ') was commited by ' . $data['author'] . ': ' . $data['message'] . "";

$message = substr($message, 0, 140);

$content = 'status=' . urlencode($message);
$packetString = "POST /statuses/update.xml HTTP/1.1\r\n";
$packetString .= "Authorization: Basic " . base64_encode($username . ":" . $password) . "\r\n";
$packetString .= "Content-Length:" . strlen($content) . "\r\n";
$packetString .= "HOST: twitter.com\r\n";
$packetString .= "\r\n" . $content . "\r\n";

$sock = fsockopen('twitter.com', 80);
fwrite($sock, $packetString);
//This is some logging, to a local file so I can monitor local what's going on
$response = fread($sock, 10240);
fwrite($fh, $packetString . "\r\n\r\n\r\n" . trim($response) . "\r\n\r\n\r\nD:\r\n" . $d);
fclose($fh);

You can see it in action here: http://twitter.com/fmsvn using a callback from our SVN server I am posting the SVN messages to the projects Twitter Feed.

Unkwntech
Why would you do raw sockets when PHP has cURL support?
ceejayoz
I like sockets, and cause I (personaly) think its easyer then cURL not to mention that there are a hand full of (crapy) shared hosting companies that don't have cURL installed.
Unkwntech
+4  A: 

Whenever you want to use HTTP basic auth with anything, if you want to ignore the actual implementation and HTTP headers, just use cURL. Here's a simple example in PHP, cURL is available in other languages too:

<?php
$ch = curl_init();
// Sets the URL cURL will open
curl_setopt($ch, CURLOPT_URL, 'http://twitter.com/statuses/user_timeline.xml?screen_name=al3x');
// Here's the HTTP auth
// The 3rd argument is your Twitter username and password joined with a colon
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
// Makes curl_exec() return server response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Lately the Twitter API expects an Expect header. It's a mystery
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
// And here's the result XML
$twitter_xml = curl_exec($curl_handle);
curl_close($ch);
?>

And then $twitter_xml will contain the XML of al3x's public timeline. As far as rate limiting goes, ceejayoz already answered that pretty well.

Justin Poliey
A: 
$twitter = file_get_content("http://user:[email protected]/blabla");

more about native HTTP Wrapper support in PHP

duckyflip