0

我是 PHP 新手,所以这可能是个愚蠢的问题,但我试图自己弄清楚但没有成功。我正在尝试使用 Zend Framework 从 Twitter 获取一些信息,这是响应:

    [jsonBody:protected] => Array
        (
            [0] => stdClass Object
                (
                    [created_at] => Sat Jun 15 08:30:00 +0000 2013
                    [id] => 3.4582047273635E+17
                    [id_str] => 345820472736354304
                    [text] => In Saturday Flashback, watch #FFC take a huge step towards     the Premier League with a vital win at Blackburn in 2001. http://t.co/iRWPDg7NmK
                    [source] => web
                    [truncated] => 
                    [in_reply_to_status_id] => 
                    [in_reply_to_status_id_str] => 
                    [in_reply_to_user_id] => 
                    [in_reply_to_user_id_str] => 
                    [in_reply_to_screen_name] => 
                    [user] => stdClass Object
                        (
                            [id] => 22903812
                            [id_str] => 22903812
                            [name] => Fulham Football Club
                            [screen_name] => FulhamFC
                            [location] => Fulham, London, England
                            [description] => The Official Twitter page of Fulham Football Club, London, England. http://t.co/GWMBbvhEfT #ffc #fulhamfc #coyw #bytheriver
                            [url] => http://t.co/qHWTVnXr2E
                            [entities] => stdClass Object
                                (
                                    [url] => stdClass Object
                                        (
                                            [urls] => Array
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [url] => http://t.co/qHWTVnXr2E
                                                            [expanded_url] => http://www.fulhamfc.com
                                                            [display_url] => fulhamfc.com
                                                            [indices] => Array
                                                                (
                                                                    [0] => 0
                                                                    [1] => 22
                                                                )

                                                        )

                                                )

                                        )

                                    [description] => stdClass Object
                                        (
                                            [urls] => Array
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [url] => http://t.co/GWMBbvhEfT
                                                            [expanded_url] => http://fulhamfc.com
                                                            [display_url] => fulhamfc.com
                                                            [indices] => Array
                                                                (
                                                                    [0] => 68
                                                                    [1] => 90
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [protected] => 
                            [followers_count] => 139667
                            [friends_count] => 12662
                            [listed_count] => 2083
                            [created_at] => Thu Mar 05 10:10:44 +0000 2009
                            [favourites_count] => 16
                            [utc_offset] => 0
                            [time_zone] => London
                            [geo_enabled] => 
                            [verified] => 1
                            [statuses_count] => 11680
                            [lang] => en
                            [contributors_enabled] => 
                            [is_translator] => 
                            [profile_background_color] => 000000
                            [profile_background_image_url] => http://a0.twimg.com/profile_background_images/837307227/ac1a6a4096d1b8cda9276d169b57b889.jpeg
                            [profile_background_image_url_https] => https://si0.twimg.com/profile_background_images/837307227/ac1a6a4096d1b8cda9276d169b57b889.jpeg
                            [profile_background_tile] => 
                            [profile_image_url] => http://a0.twimg.com/profile_images/3178979663/5ef1b3abec0dec9e160491cb62c835f5_normal.jpeg
                            [profile_image_url_https] => https://si0.twimg.com/profile_images/3178979663/5ef1b3abec0dec9e160491cb62c835f5_normal.jpeg
                            [profile_banner_url] => https://pbs.twimg.com/profile_banners/22903812/1348050191
                            [profile_link_color] => 0084B4
                            [profile_sidebar_border_color] => FFFFFF
                            [profile_sidebar_fill_color] => DDEEF6
                            [profile_text_color] => 333333
                            [profile_use_background_image] => 1
                            [default_profile] => 
                            [default_profile_image] => 
                            [following] => 
                            [follow_request_sent] => 
                            [notifications] => 
                        )

                    [geo] => 
                    [coordinates] => 
                    [place] => 
                    [contributors] => 
                    [retweet_count] => 9
                    [favorite_count] => 5
                    [entities] => stdClass Object
                        (
                            [hashtags] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [text] => FFC
                                            [indices] => Array
                                                (
                                                    [0] => 29
                                                    [1] => 33
                                                )

                                        )

                                )

                            [symbols] => Array
                                (
                                )

                            [urls] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [url] => http://t.co/iRWPDg7NmK
                                            [expanded_url] => http://ow.ly/m3Ljl
                                            [display_url] => ow.ly/m3Ljl
                                            [indices] => Array
                                                (
                                                    [0] => 117
                                                    [1] => 139
                                                )

                                        )

                                )

                            [user_mentions] => Array
                                (
                                )

                        )

                    [favorited] => 
                    [retweeted] => 
                    [possibly_sensitive] => 
                    [lang] => en
                )

        )

我没有找到访问数据的方法,将对象转换为数组不起作用,直接作为对象访问也不起作用。请指教。

例如:

echo $response['0']['text'];
echo $response->0->text;
4

2 回答 2

0

您无法访问类之外的对象或其子对象的受保护属性。

要访问 jsonBody 和 rawBody,您必须在对象上相应地使用方法 toValue() 和 getRawResponse()。方法在 Zend/Service/Twitter/Response.php 中定义。

例子:

$response = $response->toValue();
echo $response[0]->text;

另一种方法是创建一个扩展 Zend_Service_Twitter_Response 的类,以便访问对象的所有受保护属性。这种方法只是为了理解继承是如何工作的。

例子 :

class Plugin_TwitterSearch extends Zend_Service_Twitter_Response
{
    public function __construct() {}
    public function search () {
        $twitter  = new Zend_Service_Twitter($options);  //$options - your consumer and access data
        $twits = $twitter->search->tweets('#zendframework', array('count' => 100));
        var_dump($twits->jsonBody);
        var_dump($twits->rawBody);
        exit;
   }
}
于 2013-11-06T19:56:20.540 回答
-1

他们几天前更改了 API。这是如何做到的:

$accessToken = new Zend_Oauth_Token_Access();
$accessToken->setToken($config['twitter']['accessToken']);
$accessToken->setTokenSecret($config['twitter']['accessTokenSecret']);

$twitter = new Zend_Service_Twitter(
array(
                    'accessToken' => $accessToken,
                    'oauthOptions' => array(
                        'consumerKey' => $config['twitter']['consumerKey'],
                        'consumerSecret' => $config['twitter']['consumerSecret']
                    )
                )
            );

            $response = $twitter->statuses->statusesUserTimeline(array('count' => $config['twitter']['posts']));
            $response = $response->toValue();

...作为回应,你有很好的帖子数组。

于 2013-06-18T08:14:31.613 回答