Access Twitter API via PHP

I am working on a grid script with OAuth instead of Basic Auth and I'm stuck. I am currently working on authentication, but I cannot get this to work. This code:

<?php include 'config.php'; include 'twitteroauth/twitteroauth.php'; // Use config.php credentials $conn = new TwitterOAuth(CONSUMER_KEY,CONSUMER_SECRET); // Use application registered callback URL // And get temporary credentials using made connection $tempCred = $conn->getRequestToken(); // Use 'Sign in with Twitter' // for Redirect URL $rURL = $conn->getAuthorizeURL($tempCred); echo '<a href="'.$rURL.'">1. Click me first!</a><br />'; 

works just fine. However, when I do this at this step:

  // Build a new TwitterOAuth connection // Now that the app has verified credentials $conn = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); // Get non-temporary credentials from Twitter $tokenCred = $conn->getAccessToken(); echo '<a href="index.php">2. Click me next!</a><br />'; ?> 

I get a page inaccessible, Error 324 (net::ERR_EMPTY_RESPONSE): Unknown error . Is anyone familiar with this problem? I followed the documentation as much as I could, but since I'm a complete rookie, I'm sure I made some kind of stupid mistake.

Also, the next question is: as soon as I get the script authorization through this process, what will be my next step in terms of capturing xml friends feed? Can I just cURL him like before?

EDIT: source getAccessToken(); as follows:

  function getAccessToken($oauth_verifier = FALSE) { $parameters = array(); if (!empty($oauth_verifier)) { $parameters['oauth_verifier'] = $oauth_verifier; } $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters); $token = OAuthUtil::parse_parameters($request); $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); return $token; } 

And yes, config.php is correct.

+6
php oauth twitter
source share
3 answers

I used this api to authenticate oauth based on pin for twitter. I used php simple oauth library http://php.net/manual/en/book.oauth.php .

Here is the code you want to see.

 class TwitterPinBasedOauth{ private static $requestTokenUrl = 'http://twitter.com/oauth/request_token'; private static $accessTokenUrl = 'http://twitter.com/oauth/access_token'; private static $authorizeUrl = 'http://twitter.com/oauth/authorize'; private static $updateUrl = 'http://twitter.com/statuses/update.json'; private $twitterOauth; public function __construct(){ $this->twitterOauth = new OAuth(ConsumerToken::$CONSUMER_KEY, ConsumerToken::$CONSUMER_SECRET, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION); } public function getAndStoreRequestToken(){ $callbackUrl = "oob"; $response = $this->twitterOauth->getRequestToken(self::$requestTokenUrl, $callbackUrl); print_r("REQUEST TOKEN:\n"); print_r($response); print_r(PHP_EOL); file_put_contents(Constants::$oauth_request_file, serialize($response)); echo "AUTH URL:\n".self::$authorizeUrl."?oauth_token=".$response['oauth_token'].PHP_EOL; } public function getAcessToken($pin){ $request_tokens = unserialize(file_get_contents(Constants::$oauth_request_file)); $this->twitterOauth->setToken($request_tokens["oauth_token"],$request_tokens["oauth_token_secret"]); $response = $this->twitterOauth->getAccessToken(self::$accessTokenUrl, NULL, $pin); file_put_contents(Constants::$oauth_access_file, serialize($response)); print_r("ACESS TOKEN:\n"); print_r($response); print_r(PHP_EOL); } public function updateStatus($status){ try{ $access_tokens = unserialize(file_get_contents(Constants::$oauth_access_file)); $this->twitterOauth->setToken($access_tokens["oauth_token"],$access_tokens["oauth_token_secret"]); $this->twitterOauth->fetch(self::$updateUrl, array('status' => $status), OAUTH_HTTP_METHOD_POST); } catch(OAuthException $e){ error_log($e->getMessage().PHP_EOL); return intval($e->getCode()); } } } 
+1
source share

Make sure that CONSUMER_KEY and CONSUMER_SECRET match the values ​​on your application’s Twitter page.

What is the authorization URL returned from the method?

0
source share

I used this API and it worked fine for me. Nothing else comes to mind, except that it really checks that CONSUMER_KEY and CONSUMER_SECRET, as Ian Quigley said, because you say that your $ _SESSION ['oauth_token'] is empty, and this should not be. The reason that its empty might be the wrong values ​​in CONSUMER_KEY and CONSUMER_SECRET If you send the wrong keys to Twitter does not give you the session variables you need

Just check if these values ​​are identical in https://twitter.com/apps (your application data) and in the config.php file. By default, config.php contains invalid values, you need to fill in your own.

0
source share

All Articles