微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在foreach循环中将返回的JSON读入PHP可用数组时出现问题

|| 我根本不了解自己在做什么错。但是对于我来说,我无法在foreach循环中访问JSON字符串中的信息。 这是我的代码。 (json字符串包含任何给定用户的事件列表。用户ID在URL中以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,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); 
}
    ,        您可以尝试类似
$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\']}\";
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。