访问数组 In Json 响应 In PHP 来自 REST API 来自 Invision 社区的响应
Access Array In Json response In PHP from REST API response from Invision Community
我正在尝试从 Invision 社区 REST API 调用的 Json 响应内部访问“结果”数组。我不确定如何从 PHP 代码中过滤到结果数组,如下所示。一旦我过滤到它,我想将它回显到屏幕上,就像当前显示完整响应的方式一样。任何帮助将不胜感激!
PHP代码
<?php
$communityUrl = 'https://website.net/invisioncommunity';
$apiKey = 'apikey';
$endpoint = '/core/members';
$curl = curl_init( $communityUrl . 'api' . $endpoint );
curl_setopt_array( $curl, array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "{$apiKey}:"
) );
$response = curl_exec( $curl );
$resArr = array();
$resArr = json_decode($response);
$resultsArr = cleanResponse($response);
echo "<pre>"; print_r($resultsArr); echo "</pre>";
function cleanResponse($arr)
{
$element = $arr['results'];
return $element;
}
Json(部分)来自 API 调用
的响应
{
"page": 1,
"perPage": 25,
"totalResults": 111,
"totalPages": 5,
"results": [
{
"id": 1,
"name": "Demo",
"title": null,
"timeZone": "America\/New_York",
"formattedName": "Demo<\/span>",
"primaryGroup": {
"id": 4,
"name": "Demo",
"formattedName": "Demo<\/span>"
},
"secondaryGroups": [
{
"id": 15,
"name": "Root Admin (Do not edit!)",
"formattedName": "Root Admin (Do not edit!)"
}
],
"email": "demo@website.net",
"joined": "2020-07-07T19:48:33Z",
"registrationIpAddress": "",
"warningPoints": 0,
"reputationPoints": 6,
"photoUrl": "",
"photoUrlIsDefault": false,
"coverPhotoUrl": "",
"profileUrl": "https:\/\/www.website.net\/profile\/1-demo\/",
"validating": false,
"posts": 3,
"lastActivity": "2020-07-14T20:01:21Z",
"lastVisit": "2020-07-14T18:43:48Z",
"lastPost": "2020-07-13T13:26:02Z",
"profileViews": 1234,
"birthday": "1\/2",
"customFields": {
"1": {
"name": "Personal Information",
"fields": {
"1": {
"name": "About Me",
"value": ""
},
"2": {
"name": "Discord",
"value": "Demo#0000"
},
"3": {
"name": "Steam Hex",
"value": null
}
}
}
}
},
]
}
对您的代码进行一些小改动应该可以解决您的问题:
- 要返回关联数组,请使用
true
作为 json_decode()
的第二个参数
$resArr = json_decode($response, true);
- 使用关联数组
$resArr
作为函数的参数 cleanResponse
:
$resultsArr = cleanResponse($resArr);
现在 $resultsArr
包含 results
子数组,您可以使用例如输出print_r()
我正在尝试从 Invision 社区 REST API 调用的 Json 响应内部访问“结果”数组。我不确定如何从 PHP 代码中过滤到结果数组,如下所示。一旦我过滤到它,我想将它回显到屏幕上,就像当前显示完整响应的方式一样。任何帮助将不胜感激!
PHP代码
<?php
$communityUrl = 'https://website.net/invisioncommunity';
$apiKey = 'apikey';
$endpoint = '/core/members';
$curl = curl_init( $communityUrl . 'api' . $endpoint );
curl_setopt_array( $curl, array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "{$apiKey}:"
) );
$response = curl_exec( $curl );
$resArr = array();
$resArr = json_decode($response);
$resultsArr = cleanResponse($response);
echo "<pre>"; print_r($resultsArr); echo "</pre>";
function cleanResponse($arr)
{
$element = $arr['results'];
return $element;
}
Json(部分)来自 API 调用
的响应{
"page": 1,
"perPage": 25,
"totalResults": 111,
"totalPages": 5,
"results": [
{
"id": 1,
"name": "Demo",
"title": null,
"timeZone": "America\/New_York",
"formattedName": "Demo<\/span>",
"primaryGroup": {
"id": 4,
"name": "Demo",
"formattedName": "Demo<\/span>"
},
"secondaryGroups": [
{
"id": 15,
"name": "Root Admin (Do not edit!)",
"formattedName": "Root Admin (Do not edit!)"
}
],
"email": "demo@website.net",
"joined": "2020-07-07T19:48:33Z",
"registrationIpAddress": "",
"warningPoints": 0,
"reputationPoints": 6,
"photoUrl": "",
"photoUrlIsDefault": false,
"coverPhotoUrl": "",
"profileUrl": "https:\/\/www.website.net\/profile\/1-demo\/",
"validating": false,
"posts": 3,
"lastActivity": "2020-07-14T20:01:21Z",
"lastVisit": "2020-07-14T18:43:48Z",
"lastPost": "2020-07-13T13:26:02Z",
"profileViews": 1234,
"birthday": "1\/2",
"customFields": {
"1": {
"name": "Personal Information",
"fields": {
"1": {
"name": "About Me",
"value": ""
},
"2": {
"name": "Discord",
"value": "Demo#0000"
},
"3": {
"name": "Steam Hex",
"value": null
}
}
}
}
},
]
}
对您的代码进行一些小改动应该可以解决您的问题:
- 要返回关联数组,请使用
true
作为json_decode()
的第二个参数
$resArr = json_decode($response, true);
- 使用关联数组
$resArr
作为函数的参数cleanResponse
:
$resultsArr = cleanResponse($resArr);
现在 $resultsArr
包含 results
子数组,您可以使用例如输出print_r()