通过PHP访问Twitter API

And*_*ndy 6 php twitter oauth

我正在努力使我的脚本网格与OAuth而不是Basic Auth,我被卡住了.到目前为止,我现在只是在进行身份验证,但我无法做到这一点.这段代码:

<?php

  include 'config.php';
  include 'twitteroauth/twitteroauth.php';

  // Use config.php credentials
  $conn = new TwitterOAuth(CONSUMER_KEY,CONSUMER_SECRET);

  // Use application's 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 />';
Run Code Online (Sandbox Code Playgroud)

工作得很好.但是,当我进入这一步时:

  // 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 />';

?>
Run Code Online (Sandbox Code Playgroud)

我得到一个页面不可用,Error 324 (net::ERR_EMPTY_RESPONSE): Unknown error.有人熟悉这个问题吗?我尽可能地密切关注文档,但由于我是一个完整的新手,我确信我犯了一些愚蠢的错误.

另外,一个跟进问题:一旦我通过这个过程获得我的脚本授权,在获取朋友提要方面我的下一步是什么xml?我cURL可以像之前一样吗?

编辑:来源getAccessToken();如下:

 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;
  }
Run Code Online (Sandbox Code Playgroud)

是的,config.php是正确的.

Mas*_*oda 1

我已使用此 api 对 twitter 进行基于 pin 的 oauth 身份验证。我使用 php 简单 oauth 库http://php.net/manual/en/book.oauth.php

如果您想查看的话,这是代码。



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());
        }
   }
} 

Run Code Online (Sandbox Code Playgroud)