XenAPI - How to convert API response to PHP Variable

Nyx23

New Member
#1
I am a newbie on PHP programming, currently i trying to integrate a Xenforo forum with my website. I would like to integrate the registration system use XenAPI plugin. I follow the guide from this page and it works!

Example request:
Mã:
api.php?action=register&hash=d8e8fca2dc0f896fd7cb4cb0031ba249&username=Contex&password=My_Password&email=me@contex.me
And the response is:
Mã:
{
  "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"
}
Here is my question, how to convert one of the response to be a PHP Variable?

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

New Question

I get an answer from Hassaan and i follow his instruction,

I edit my php script to be like below:
Mã:
$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;
And i test it and get a new error message.
Mã:
Notice: Trying to get property of non-object.
How to fix it? Thanks!
 

Admin

Administrator
Thành viên BQT
#2
You need to use json_decode()
Mã:
$response = json_decode($str);
$user_group_id = $response->user_group_id;

echo $user_group_id;
Note Replace $str with your API response variable.

Try Example
Mã:
$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;
 
OP
OP
N

Nyx23

New Member
#3
Hello, thanks for your answer.. Now i just get a new problem. I get response Notice: Trying to get property of non-object. How to fix it? Thanks!
 
OP
OP
N

Nyx23

New Member
#5
Hello, i just realize that the decoded response is a multi-dimensional array. So i can successfully call the variable with this code -> echo $response[0]->user_id; Thanks for your answer.
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top