在foreach循环中读取返回的JSON到PHP可用数组的问题
我真的不明白我做错了什么。但是对于我来说,我无法在foreach循环中访问JSON字符串中的信息。在foreach循环中读取返回的JSON到PHP可用数组的问题
这是我的代码。 (json字符串包含任何给定用户的事件列表,用户标识作为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和Facebook的事件和地点。
在此先感谢!
这是错误的:
foreach(json_decode($json) as $obj) {
}
它应该是:
$json = json_decode($json);
foreach ($json->data as $data) {
echo $data->id . ' ' . htmlspecialchars($data->owner->name);
}
Hi Kieran, 您的回应非常完美! 我加了建议如下...... foreach($ json-> data as $ data){ echo $ data-> id。 ' - '。 htmlspecialchars($ data-> owner-> name)。“
”; } 我的输出现在是一个非常干净可用的数组,我可以遍历和做什么,我希望(这将是在我的本地MySQL数据库事件同步) 输出(基兰的变化后) 159231904144232 - 迪恩·赫利 118572044893404 - RAVEOLOGY 123371994410260 - 该Bozeat市压路机 316743171061 - 理查德·约翰斯通 160599624007096 - 迪恩·赫利 203743706335174 - Bozeat红狮 –
2011-06-12 11:25:43
玩得开心! Facebook Graph API实际上很棒,一旦你克服了最初的障碍(缺少文档,bug等) – 2011-06-12 11:41:16
是的,我同意,我没有尽我所能地使用JSON。我对XML更加熟悉。这已经克服了我的项目中的一个主要障碍,为此我真的很棒! – 2011-06-12 11:47:17
你可以尝试像
$json = json_decode($json);
foreach ($json as $item) {
var_dump($item);
}
,而不是直接把函数在foreach。
我不明白你的问题是什么:
$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']}";
}
是否有一个原因,为什么你不能访问它?如果是这样,请告诉我们。 – hakre 2011-06-12 11:04:15
我们是否可以看到那些不起作用的代码,而不是那一点呢? – Eric 2011-06-12 11:07:54