如何从 Twitter JSON 数组中提取值

发布于 2024-11-07 12:49:26 字数 895 浏览 0 评论 0原文

我正在创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗳卜坏 2024-11-14 12:49:26

经过 json_decode 编辑后,它只是一个 PHP 数组,对吧?

$code->relationship->target->id

或者

$code['relationship']['target']['id']

It's just a PHP array after it's been json_decodeed, right?

$code->relationship->target->id

or

$code['relationship']['target']['id']
行至春深 2024-11-14 12:49:26
$code['relationship']['target']['id']

看一下示例:http://codepad.org/KMEP5wPW

$code['relationship']['target']['id']

Look at example: http://codepad.org/KMEP5wPW

送舟行 2024-11-14 12:49:26

您已经应用了 json_decode(),因此您正在讨论的值应该可以通过以下代码访问:

$id_needed = $code['relationship']['target']['id'];

因为仅当 $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:

$id_needed = $code['relationship']['target']['id'];

Because you have overwritten $code only when $code == 200, you have to assume $code may be something different than result from json_decode().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文