Jump to content

PHP + cURL How to post to Twitter "like a web browser"

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Mysteriis

Mysteriis

    Newbie

  • Members
  • Pip
  • 5 posts
Hello, my first topic here. I am trying to create a twitter tool in PHP that would allow me to post to Twitter, it's in PHP with cURL.
I don't want to use their crappy API so I'm trying to emulate what a web browser would do.

I can connect but can't post, I get this error : {"errors":[{"code":37,"message":"Not authorized to use this endpoint"}]}

I compared my request with the web browser's request on Wireshark and they seem pretty similar.

Does anyone have an idea on how to fix this ?

here is my code :
function tw_connect($user, $pwd) {

	$ch = curl_init('http://www.twitter.com');

	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

	$c = curl_exec($ch);

	curl_close($ch);

	preg_match_all('#authenticity_token" type="hidden" value="(.*)"#U', $c, $g);

	$g = $g[1][0];


	$post = array('authenticity_token' => $g, 'return_to_ssl' => 'false', 'redirect_after_login' => '/', 'session[username_or_email]' => $user, 'session[password]' => $pwd, 'q' => null);

	$ch = curl_init('https://twitter.com/sessions');

	curl_setopt($ch, CURLOPT_POST, TRUE);

	curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

	curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:' ) );

	curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);

	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

	$c = curl_exec($ch);

	curl_close($ch);

	echo $c;

}

function tw_submit($user, $pwd, $text) {

	tw_connect($user, $pwd);

	$ch = curl_init('http://www.twitter.com/');

	curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

	$c = curl_exec($ch);

	curl_close($ch);

	preg_match_all('#value=\'(.*)\' name=\'authenticity_token#U', $c, $g);

	$g = $g[1][0];


	$post = 'include_entities=true&status='.$text.'&post_authenticity_token='.$g;

	$ch = curl_init('http://api.twitter.com/1/statuses/update.json');

	curl_setopt($ch, CURLOPT_POST, TRUE);

	curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

	curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; fr; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTBDFff GTB7.0 (.NET CLR 3.5.30729)');

	curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-Requested-With: XMLHttpRequest', 'X-PHX: true', 'Referer: http://api.twitter.c..._receiver.html' ) );

	curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);

	curl_setopt($ch, CURLOPT_USERPWD, "$user:$pwd");

	//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

	$c = curl_exec($ch);

	curl_close($ch);

	echo $c;

}

tw_submit('username', 'password', 'your text to send to twitter');


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Your best bet would be to look at the Twitter API and learn what that error means, it is much easier to go from there rather than assuming matching the client will work (the authentication could require further info)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Mysteriis

Mysteriis

    Newbie

  • Members
  • Pip
  • 5 posts
Hello.

First of all, thanks for your response.

But I can't seem to find any documentation on this particular error on the twitter API.

What do you mean by "(the authentication could require further info)" ? That it might require more cookies than I actually send for example ?

Thanks !

#4
__ak

__ak

    Newbie

  • Members
  • PipPip
  • 24 posts
Maybe you can find some help in this post?
Issue 2114 - twitter-api - API returning "Not authorized to use this endpoint" - Twitter's API Issue Tracker - Google Project Hosting

Seems like it could have something to do with ssl.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users