2013-01-09 51 views
1

我花了我的最後5個小時在這個問題上,最後我來到這裏的解決方案。Twitter OAuth返回空白數組,如果沒有鳴叫

我正在使用我的網站(Zend Framework + PHP)中的twitter功能進行登錄,並且一切工作正常。但我面對它了以下問題:

如果用戶在他的帳戶沒有鳴叫(0微博)則

$tweets = json_decode($response->getBody()); 
echo "<pre>"; 
print_r($tweets); 
exit; 

它顯示我的空白陣列。即:Array(); :-(

如果我在Twitter帳戶加入一些微博有那麼它顯示我喜歡的顯示名稱,圖像等用戶信息沿整個陣列...像這樣:

Array 
(
    //other data 
    [0] => stdClass Object 
     (
      [user] => stdClass Object 
      .... 
      .... 
      so on.. 
     ) 
) 

以下是我的代碼:

public function twitterregisterAction() { 
     $path = realpath(APPLICATION_PATH . '/../library/'); 
     set_include_path($path); 
     session_start(); 
     require $path . "/Zend/Oauth/Consumer.php"; 

     $config = array(
      "callbackUrl" => "http://" . $_SERVER['HTTP_HOST'] . "/register/twittercallback", 
      "siteUrl" => "http://twitter.com/oauth", 
      "consumerKey" => "xxxxxxxxxxxxx", 
      "consumerSecret" => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
     ); 
     $consumer = new Zend_Oauth_Consumer($config); 
// fetch a request token 
     $token = $consumer->getRequestToken(); 

// persist the token to storage 
     $_SESSION["TWITTER_REQUEST_TOKEN"] = serialize($token); 
// redirect the user 
     $consumer->redirect(); 
    } 

    /* 
    * Ticket id #16 
    * twittercallbackAction method 
    */ 

    public function twittercallbackAction() { 
     $config = array(
      "callbackUrl" => "http://" . $_SERVER['HTTP_HOST'] . "/register/twittercallback", 
      "siteUrl" => "http://twitter.com/oauth", 
      "consumerKey" => "xxxxxxxxxxxxxxxxxx", 
      "consumerSecret" => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
     ); 
     $consumer = new Zend_Oauth_Consumer($config); 

     if (!$this->_getParam("denied")) { 

      if (!empty($_GET) && isset($_SESSION['TWITTER_REQUEST_TOKEN'])) { 
       $token = $consumer->getAccessToken($_GET, unserialize($_SESSION['TWITTER_REQUEST_TOKEN'])); 
      } else { 
// Mistaken request? Some malfeasant trying something? 
       exit('Invalid callback request. Oops. Sorry.'); 
      } 
// save token to file 
// file_put_contents('token.txt', serialize($token)); 
      $client = $token->getHttpClient($config); 
      $client->setUri('https://api.twitter.com/1/statuses/user_timeline.json?'); 
      $client->setMethod(Zend_Http_Client::GET); 
      $client->setParameterGet('name'); 
      $client->setParameterGet('profile_image_url'); 
      $response = $client->request(); 
      $tweets = json_decode($response->getBody()); 

      $session = new Zend_Session_Namespace("userIdentity"); 
      Zend_Session::rememberMe(63072000); //2years 
      $session->tw_id = $tweets[0]->user->id; 
      $session->tw_name = $tweets[0]->user->name; 
      $session->tw_image = $tweets[0]->user->profile_image_url; 

      if ($session->tw_id != "") { 
       $tw_id = $session->tw_id; 
//Calling the function twitterAuthAction for email authentication 
       $twAuthArr = $this->twitterAuthAction($tw_id); 
       if ($twAuthArr['socialId'] == $tw_id) { 
        $session->userId = $twAuthArr['id']; 
        $session->email = $twAuthArr['emailId']; 
        $this->_redirect('/profile/showprofile'); 
       } else { 
        $user = new UserRegistration(); 
        $firstname = ""; 
        $lastname = ""; 
        $password = ""; 
        $socialtype = "twitter"; 
        $email = ""; 
        $socialid = $session->tw_id; 
        $result = $user->registerUser($firstname, $lastname, $socialid, $socialtype, $email, $password); 
        $session->userId = $result; 
        $this->_redirect('/register'); 
       } 
      } 
      $this->_redirect("/register"); 
     } else { 
      $this->_redirect("/register"); 
     } 
    } 

我的問題是:

1)爲什麼它不提供用戶陣如果我的Twitter帳戶中沒有任何推文(或新建立的推特帳戶)

2)我想要twitter帳戶中的用戶個人資料詳細信息。我怎麼才能得到它?

需要幫助。謝謝

回答

1

我認爲根據大衛的回答,你需要使用users/show url那裏,而不是使用statuses/user_timeline。您可以使用curl請求url,以便獲得包含用戶信息的響應。

嘗試用下面的代碼:

$user_id = $client->getToken()->getParam('user_id'); 
    $trends_url = "http://api.twitter.com/1/users/show.json?user_id=".$user_id; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $trends_url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $curlout = curl_exec($ch); 
    curl_close($ch);  
    $response = json_decode($curlout, true); 

    $session = new Zend_Session_Namespace("userIdentity"); 
    Zend_Session::rememberMe(63072000); //2years 
    $session->tw_id = $response['id']; 
    $session->tw_name = $response['name']; 
    $session->tw_image = $response['profile_image_url']; 

試試這個。希望它能幫助你。

+0

它的工作正是我想要的!太好了!謝謝 – Sky

1

我認爲你誤讀了statuses/user_timeline端點的Twitter API文檔。

您確定的字段user是返回的推文中的字段之一。如果您指向的用戶標識沒有推文,則返回的數組中不會有條目,因此沒有用戶字段。

如果您即使在沒有任何推文的情況下也需要用戶信息,那麼您可能需要點擊users/show端點。

相關問題