如何从 Twitter JSON 数组中提取值
我正在创建一个 php 脚本来检查是否有人在 Twitter 上关注我,并且我正在使用 REST API 方法:友谊显示方法 并使用 themattHarris tmhOAuth 库进行 OAuth。
我使用以下代码发出请求:
$code=$tmhOAuth->request('GET', $tmhOAuth->url('friendships/show'), array( 'target_screen_name' => 'bob' ));
if ($code==200){ $code = json_decode($tmhOAuth->response['response'], true); }
请求成功并返回类似 json 输出,如 apiwiki 页面所示:
{"relationship": { "source": { "id": 123, "screen_name": "bob", "following": true, "followed_by": false, "notifications_enabled": false }, "target": { "id": 456, "screen_name": "jack", "following": false, "followed_by": true, "notifications_enabled": null } } }
我的问题
如何从返回的数组中提取以下值?
"id": 456,
I am creating a php script to check if a person is following me on Twitter and I am using the REST API Method: friendships show method and using themattHarris tmhOAuth library for OAuth.
I am making the request using the following code:
$code=$tmhOAuth->request('GET', $tmhOAuth->url('friendships/show'), array( 'target_screen_name' => 'bob' ));
if ($code==200){ $code = json_decode($tmhOAuth->response['response'], true); }
The request is successful and returns similar json output as shown on the apiwiki page:
{"relationship": { "source": { "id": 123, "screen_name": "bob", "following": true, "followed_by": false, "notifications_enabled": false }, "target": { "id": 456, "screen_name": "jack", "following": false, "followed_by": true, "notifications_enabled": null } } }
MY QUESTION
How do I extract the following value from the array returned?
"id": 456,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过
json_decode
编辑后,它只是一个 PHP 数组,对吧?或者
It's just a PHP array after it's been
json_decode
ed, right?or
看一下示例:http://codepad.org/KMEP5wPW
Look at example: http://codepad.org/KMEP5wPW
您已经应用了
json_decode()
,因此您正在讨论的值应该可以通过以下代码访问:因为仅当
$code = 时您才覆盖
,您必须假设$code
= 200$code
可能与json_decode()
的结果不同。You already applied
json_decode()
, so the value you are talking about should be accessible by the following code:Because you have overwritten
$code
only when$code == 200
, you have to assume$code
may be something different than result fromjson_decode()
.