如何将API响应转换为PHP变量


XenAPI - How to convert API response to PHP Variable

我是PHP编程的新手,目前我试图将Xenforo forum与我的网站集成。我想用XenAPI plugin来集成注册系统。我遵循这个页面的指南,它工作!

示例请求:

api.php?action=register&hash=d8e8fca2dc0f896fd7cb4cb0031ba249&username=Contex&password=My_Password&email=me@contex.me

响应是:

{
  "visible": 1,
  "user_group_id": 2,
  "user_state": "valid",
  "language_id": 1,
  "username": "Contex",
  "email": "me@contex.me",
  "timezone": "Europe'/Berlin",
  "gender": "male",
  "style_id": 0,
  "secondary_group_ids": "",
  "display_style_group_id": 2,
  "permission_combination_id": 2,
  "message_count": 0,
  "alerts_unread": 0,
  "conversations_unread": 0,
  "register_date": 1368197942,
  "last_activity": 1368197942,
  "trophy_points": 0,
  "avatar_date": 0,
  "avatar_width": 0,
  "avatar_height": 0,
  "gravatar": "",
  "is_moderator": 0,
  "is_admin": 0,
  "is_banned": 0,
  "like_count": 0,
  "custom_title": "",
  "warning_points": 0,
  "user_id": 5,
  "dob_day": 27,
  "dob_month": 12,
  "dob_year": 1980,
  "csrf_token": "f5c36cd98569ae380b4c0383d8310954514ac64b",
  "status": "",
  "status_date": 0,
  "status_profile_post_id": 0,
  "signature": "",
  "homepage": "",
  "location": "",
  "occupation": "",
  "following": "",
  "ignored": "",
  "avatar_crop_x": 0,
  "avatar_crop_y": 0,
  "about": "",
  "facebook_auth_id": 0,
  "custom_fields": "",
  "content_show_signature": 1,
  "show_dob_date": 1,
  "show_dob_year": 1,
  "receive_admin_email": 1,
  "email_on_conversation": 1,
  "default_watch_state": "watch_email",
  "is_discouraged": 0,
  "alert_optout": "",
  "enable_rte": 1,
  "allow_view_profile": "everyone",
  "allow_post_profile": "members",
  "allow_receive_news_feed": "everyone",
  "allow_send_personal_conversation": "members",
  "allow_view_identities": "everyone",
  "scheme_class": "XenForo_Authentication_Core",
  "data": "a:3:{s:4:'"hash'";s:64:'"f1a9f22e79366a9d7b8fcf18178d7bc95c8057ec6f8904b48809f24c2f40efdb'";s:4:'"salt'";s:64:'"f30d7f122f8db2f2c9f92755a667149ed524ae50ab1c0e2f269c4588f8e5835a'";s:8:'"hashFunc'";s:6:'"sha256'";}",
  "remember_key": "11d20c2e9ffbbd7466a872dacf18051a47d94dce"
}

这是我的问题,如何转换响应之一为PHP变量?

From "user_group_id": 2 to $user_group_id = 2 .

新问题

我从Hassaan那里得到了答案,我遵照他的指示,

我编辑我的php脚本如下:

$response = file_get_contents('http://forum.mydomain.com/api.php?action=getUsers&value=Contex&hash=Contex:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08');
$response = json_decode($response, true);
echo $response->user_id;

然后我测试它,得到一个新的错误信息。

Notice: Trying to get property of non-object.

如何修复?谢谢!

您需要使用json_decode()

$response = json_decode($str);
$user_group_id = $response->user_group_id;
echo $user_group_id;

注释用API响应变量替换$str

试例

$str = <<<JSON
{
  "visible": 1,
  "user_group_id": 2,
  "user_state": "valid",
  "language_id": 1,
  "username": "Contex",
  "email": "me@contex.me",
  "timezone": "Europe'/Berlin",
  "gender": "male",
  "style_id": 0,
  "secondary_group_ids": "",
  "display_style_group_id": 2,
  "permission_combination_id": 2,
  "message_count": 0,
  "alerts_unread": 0,
  "conversations_unread": 0,
  "register_date": 1368197942,
  "last_activity": 1368197942,
  "trophy_points": 0,
  "avatar_date": 0,
  "avatar_width": 0,
  "avatar_height": 0,
  "gravatar": "",
  "is_moderator": 0,
  "is_admin": 0,
  "is_banned": 0,
  "like_count": 0,
  "custom_title": "",
  "warning_points": 0,
  "user_id": 5,
  "dob_day": 27,
  "dob_month": 12,
  "dob_year": 1980,
  "csrf_token": "f5c36cd98569ae380b4c0383d8310954514ac64b",
  "status": "",
  "status_date": 0,
  "status_profile_post_id": 0,
  "signature": "",
  "homepage": "",
  "location": "",
  "occupation": "",
  "following": "",
  "ignored": "",
  "avatar_crop_x": 0,
  "avatar_crop_y": 0,
  "about": "",
  "facebook_auth_id": 0,
  "custom_fields": "",
  "content_show_signature": 1,
  "show_dob_date": 1,
  "show_dob_year": 1,
  "receive_admin_email": 1,
  "email_on_conversation": 1,
  "default_watch_state": "watch_email",
  "is_discouraged": 0,
  "alert_optout": "",
  "enable_rte": 1,
  "allow_view_profile": "everyone",
  "allow_post_profile": "members",
  "allow_receive_news_feed": "everyone",
  "allow_send_personal_conversation": "members",
  "allow_view_identities": "everyone",
  "scheme_class": "XenForo_Authentication_Core",
  "data": "a:3:{s:4:'"hash'";s:64:'"f1a9f22e79366a9d7b8fcf18178d7bc95c8057ec6f8904b48809f24c2f40efdb'";s:4:'"salt'";s:64:'"f30d7f122f8db2f2c9f92755a667149ed524ae50ab1c0e2f269c4588f8e5835a'";s:8:'"hashFunc'";s:6:'"sha256'";}",
  "remember_key": "11d20c2e9ffbbd7466a872dacf18051a47d94dce"
}
JSON;

$response = json_decode($str);
$user_group_id = $response->user_group_id;
echo $user_group_id;