Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果没有tweet,Twitter OAuth将返回空白数组_Php_Zend Framework_Twitter_Twitter Oauth - Fatal编程技术网

Php 如果没有tweet,Twitter OAuth将返回空白数组

Php 如果没有tweet,Twitter OAuth将返回空白数组,php,zend-framework,twitter,twitter-oauth,Php,Zend Framework,Twitter,Twitter Oauth,我在这个问题上花了最后5个小时,最后我来到这里寻求解决方案 我正在我的站点(Zend Framework+PHP)中使用twitter功能登录,一切正常。但我在其中面临以下问题: 如果用户帐户中没有tweets(0条tweets),则 $tweets = json_decode($response->getBody()); echo "<pre>"; print_r($tweets); exit; 以下是我的代码: $user_id = $client->get

我在这个问题上花了最后5个小时,最后我来到这里寻求解决方案

我正在我的站点(Zend Framework+PHP)中使用twitter功能登录,一切正常。但我在其中面临以下问题:

如果用户帐户中没有tweets(0条tweets),则

$tweets = json_decode($response->getBody());
echo "<pre>";
print_r($tweets);
exit;
以下是我的代码:

    $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'];
我的问题是:

1) 如果我的twitter帐户(或新创建的twitter帐户)中没有任何tweet,为什么它不提供用户数组

2) 我想从twitter帐户获得用户档案详细信息。我怎样才能得到它?


我需要帮助。谢谢

我认为您误读了端点的Twitter API文档

您标识的
user
字段是返回tweet中的一个字段。如果您指向的用户id没有tweet,那么返回的数组中将没有条目,因此没有用户字段


如果你需要用户信息,即使没有任何tweet,那么你可能需要点击端点。

根据david的回答,我认为你需要在那里使用
用户/show url
,而不是使用
状态/user\u时间线
。您可以使用curl请求url,这样您将得到包含用户信息的响应

请尝试使用以下代码:

试试这个。希望它能帮助你

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");
        }
    }
    $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'];