在 foreach 循环中将返回的 JSON 读取到 PHP 可用数组中时出现问题

问题描述 投票:0回答:3

我真的不明白我做错了什么。但对于我来说,我无法在 foreach 循环中访问 JSON 字符串中的信息。

这是我的代码。 (json 字符串包含任何给定用户的事件列表。用户 ID 作为 user=xxxxxx 在 URL 中传递)。

直播网址在这里http://livemuzik.co.uk/fb3.php

代码:

include('fb/src/facebook.php');

if ($_REQUEST["user"]){
$user_id = $_REQUEST["user"];
} else $user_id = "me";

$app_id = "xxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "http://livemuzik.co.uk/fb2.php"; 

$code = $_REQUEST["code"];

if(empty($code)) {
    $auth_url = "http://www.facebook.com/dialog/oauth?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url)
    . "&scope=create_event,user_events,friends_events";
    echo("<script>top.location.href='" . $auth_url . "'</script>");
}

$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);

// GET JSON DATA FOR EVENTS
  $events_url  = "https://graph.facebook.com/";
  $events_url .= $user_id;
  $events_url .= '/events?fields=id,owner&limit=100&';
  $events_url .= $access_token;

$json = file_get_contents($events_url,0,null,null);

var_dump(json_decode($json, true));

// foreach(json_decode($json) as $obj) {
// Need to work out what code to put in here or what else I can do to output the json information as single variable values from an array
// }

返回的 JSON 如下所示:

array(2) {
  ["data"] => array(8) {
    [0] => array(4) {
      ["id"] => string(15) "159231904144232"
      ["owner"] => array(2) {
        ["name"]=> string(11) "Dean Hurley"
        ["id"]=> string(10) "1038439076"
      }
      ["start_time"]=> string(24) "2011-07-15T04:00:00+0000"
      ["rsvp_status"]=> string(9) "attending"
    }
    [1]=> array(4) { ["id"]=> string(15) "118572044893404" ["owner"]=> array(3) { ["name"]=> string(9) "RAVEOLOGY" ["category"]=> string(13) "Musician/band" ["id"]=> string(10) "8443998018" } ["start_time"]=> string(24) "2011-07-10T04:00:00+0000" ["rsvp_status"]=> string(6) "unsure" } [2]=> array(4) { ["id"]=> string(15) "123371994410260" ["owner"]=> array(3) { ["version"]=> int(1) ["name"]=> string(23) "The Bozeat City Rollers" ["id"]=> string(15) "182725898429985" } ["start_time"]=> string(24) "2011-07-03T04:00:00+0000" ["rsvp_status"]=> string(9) "attending" } [3]=> array(4) { ["id"]=> string(12) "316743171061" ["owner"]=> array(2) { ["name"]=> string(17) "Richard Johnstone" ["id"]=> string(9) "668431151" } ["start_time"]=> string(24) "2011-07-01T23:00:00+0000" ["rsvp_status"]=> string(6) "unsure" } [4]=> array(4) { ["id"]=> string(15) "160599624007096" ["owner"]=> array(2) { ["name"]=> string(11) "Dean Hurley" ["id"]=> string(10) "1038439076" } ["start_time"]=> string(24) "2011-06-15T08:30:00+0000" ["rsvp_status"]=> string(9) "attending" } [5]=> array(4) { ["id"]=> string(15) "231851770163680" ["owner"]=> array(2) { ["name"]=> string(11) "Dean Hurley" ["id"]=> string(10) "1038439076" } ["start_time"]=> string(24) "2011-06-13T08:30:00+0000" ["rsvp_status"]=> string(9) "attending" } [6]=> array(4) { ["id"]=> string(15) "203743706335174" ["owner"]=> array(2) { ["name"]=> string(15) "Bozeat Red Lion" ["id"]=> string(12) "155723533443" } ["start_time"]=> string(24) "2011-06-09T16:00:00+0000" ["rsvp_status"]=> string(9) "attending" } [7]=> array(4) { ["id"]=> string(15) "208151712549353" ["owner"]=> array(3) { ["name"]=> string(20) "Blackbush Promotions" ["category"]=> string(13) "Musician/band" ["id"]=> string(12) "336182297448" } ["start_time"]=> string(24) "2011-06-05T02:30:00+0000" ["rsvp_status"]=> string(6) "unsure" } } ["paging"]=> array(2) { ["previous"]=> string(181) "https://graph.facebook.com/1038439076/events?fields=id%2Cowner&limit=100&access_token=331843765383|b9ef3b78db6a708b1a735347.1-1038439076|DkL44VVbAPlHl8mb03P1WA9VF_o&since=1310702400" ["next"]=> string(181) "https://graph.facebook.com/1038439076/events?fields=id%2Cowner&limit=100&access_token=331843765383|b9ef3b78db6a708b1a735347.1-1038439076|DkL44VVbAPlHl8mb03P1WA9VF_o&until=1307241000" } }

任何帮助将不胜感激...如果有人正在寻找与 Facebook 活动和地点相关的类似项目,请随时与我联系。我很高兴分享我的代码!

我的项目涉及通过 Joomla Events 管理组件将事件和地点与 Facebook 同步。

提前致谢!

php json joomla
3个回答
1
投票

这是错误的:

foreach(json_decode($json) as $obj) {

}

应该是:

$json = json_decode($json);

foreach ($json->data as $data) {    
    echo $data->id . ' ' . htmlspecialchars($data->owner->name); 
}

0
投票

你可以尝试类似的事情

$json = json_decode($json);
foreach ($json as $item) {
  var_dump($item);
}

而不是直接将函数放在foreach中。


0
投票

我不明白你的问题是什么:

$events = json_decode($json, true)['data'];

foreach($events as $event) {
    echo "Owner: {$event['owner']['name']}, Id: {$event['owner']['id']}";
    echo "Start time: {$event['start_time']}";
}
© www.soinside.com 2019 - 2024. All rights reserved.