2016-01-08 61 views
1

你好,我试图连接到twitter来阅读状态。 我可以发布他们就好。为什么我无法处理twitter搜索API响应php

我被卡住了,因为我收到的回复不是json,而是一个php对象。

我使用codebird库连接

\Codebird\Codebird::setConsumerKey($key, $secret); 
    $cb = \Codebird\Codebird::getInstance(); 

    $cb->setToken($token, $tokenSecret); 

    $reply = $cb->statuses_homeTimeline(); 

,我得到的是不是JSON像我期望它的数据。相反,它是一个对象

var_dump($reply); 

object(stdClass)#383 (22) { [0]=> object(stdClass)#2 (26) { ["created_at"]=> string(30) "Fri Jan 08 16:36:03 +0000 2016" ["id"]=> string(18) "685500222852710401" ["id_str"]=> string(18) "685500222852710401" ["text"]=> string(142) "Check out the views from up here! Francesco Di Tommaso looks like he knows the best place for a good photo – sa... 

json_decode($reply); 

Warning: json_decode() expects parameter 1 to be string, 

我都Python和C#之前使用Twitter的API,但从来没有遇到过什么喜欢这个。

编辑 文档说投给数组其结果是

array(22) { [0]=> object(stdClass)#2 (26) { ["created_at"]=> string(30) "Fri Jan 08 17:20:00 +0000 2016" ["id"]=> string(18) "685511281630035968" ["id_str"]=> string(18) "685511281630035968" ["text"]=> string(129) "RT @ColumbiaRecords: We've got that #FridayFeeling listening to @wet's new track #AllTheWays on @Spotify! https://t.co/zLTCUJZ03X" ["source"]=> string(83) "TweetDeck" ["truncated"]=> bool(false) ["in_reply_to_status_id"]=> NULL ["in_reply_to_status_id_str"]=> NULL ["in_reply_to_user_id"]=> NULL ["in_reply_to_user_id_str"]=> NULL ["in_reply_to_screen_name"]=> NULL ["user"]=> object(stdClass)#3 (41) { ["id"]=> int(34442404) ["id_str"]=> string(8) "34442404" ["name"]=> string(4) "Sony" ["screen_name"]=> string(4) "Sony" ["location"]=> string(12) "New York, NY" ["description"]=> string(59) "The official Twitter account for Sony in the United States."... 

对此我仍然无法转换成JSON。

如果我的问题听起来很愚蠢请原谅我我对PHP很陌生。

回答

2

Codebird正在返回一个php对象,而不是一个json字符串。 Codebird documentation在其返回格式部分中特别说明:

API调用的默认返回格式是PHP对象。对于API方法返回多个数据(如状态/ home_timeline),你应该投的答复阵列,像这样:

$reply = $cb->statuses_homeTimeline(); 
$data = (array) $reply; 
+0

我想,但我不知道怎么用它做任何。我真的很陌生,我会编辑问题来包含它。我无法将该数组转换为Json。 – xerotolerant

+1

'json_decode()'将*从* json转换为php值。 Codebird已经为你提供了php值。没有什么需要解码的。如果你想把*转换成* json,那么你需要使用'json_encode()'。 – jbafford

+0

哦,我明白了。谢谢。这工作。 – xerotolerant