Php “如何显示公司名称”;只有";关于好友工作历史(facebook api)

Php “如何显示公司名称”;只有";关于好友工作历史(facebook api),php,facebook,facebook-graph-api,facebook-php-sdk,Php,Facebook,Facebook Graph Api,Facebook Php Sdk,我一直在获取我的facebook好友的工作记录。但结果是一个数组,其内容如下: { "id": "xxx", "friends": { "data": [ { "name": "Indiarto Priadi", "work": [ { "employer": { "id": "111178415566505", "name": "Liputan 6 SCTV" } }

我一直在获取我的facebook好友的工作记录。但结果是一个数组,其内容如下:

 {
 "id": "xxx", 
 "friends": {
"data": [
  {
    "name": "Indiarto Priadi", 
    "work": [
      {
        "employer": {
          "id": "111178415566505", 
          "name": "Liputan 6 SCTV"
        }
      }, 
      {
        "employer": {
          "id": "107900732566334", 
          "name": "SCTV"
        }
      }
    ], 
    "id": "502163984"
  }, 
  {
    "name": "Agustin Kertawijaya", 
    "work": [
      {
        "employer": {
          "id": "138215336225242", 
          "name": "Trader Corporation (Canada)"
        }, 
        "location": {
          "id": "110941395597405", 
          "name": "Toronto, Ontario"
        }, 
        "position": {
          "id": "168673399850791", 
          "name": "Desktop operator <Pre press>"
        }, 
        "start_date": "2006-06-01", 
        "end_date": "2008-06-01"
      }, 
      {
        "employer": {
          "id": "114464911939560", 
          "name": "Merrill Corporation Canada"
        }, 
        "location": {
          "id": "110941395597405", 
          "name": "Toronto, Ontario"
        }, 
        "position": {
          "id": "190075304347365", 
          "name": "Financial Print Project Manager"
        }, 
        "start_date": "2006-06-01", 
        "end_date": "2011-10-01"
      }
    ], 
    "id": "511990261"
  }
], 
"paging": {
  "next": "https://graph.facebook.com/1065251285/friends?limit=2&fields=name,work&offset=2&__after_id=511990261"
}
{
“id”:“xxx”,
“朋友”:{
“数据”:[
{
“姓名”:“Indiarto Priadi”,
“工作”:[
{
“雇主”:{
“id”:“111178415566505”,
“名称”:“立普坦6 SCTV”
}
}, 
{
“雇主”:{
“id”:“107900732566334”,
“名称”:“SCTV”
}
}
], 
“id”:“502163984”
}, 
{
“名称”:“Agustin Kertawijaya”,
“工作”:[
{
“雇主”:{
“id”:“138215336225242”,
“名称”:“贸易公司(加拿大)”
}, 
“地点”:{
“id”:“110941395597405”,
“名称”:“安大略省多伦多市”
}, 
“职位”:{
“id”:“168673399850791”,
“名称”:“桌面操作员”
}, 
“开始日期”:“2006-06-01”,
“结束日期”:“2008-06-01”
}, 
{
“雇主”:{
“id”:“114464911939560”,
“名称”:“美林加拿大公司”
}, 
“地点”:{
“id”:“110941395597405”,
“名称”:“安大略省多伦多市”
}, 
“职位”:{
“id”:“190075304347365”,
“名称”:“财务打印项目经理”
}, 
“开始日期”:“2006-06-01”,
“结束日期”:“2011-10-01”
}
], 
“id”:“511990261”
}
], 
“分页”:{
“下一步”:https://graph.facebook.com/1065251285/friends?limit=2&fields=name,工时和偏移量=2&u后,_id=511990261“
}
} }

现在我想在我的页面上“只”显示雇主的名字,但我找不到这样做的方法。 这是我的密码:

$userFriends = $facebook->api('/'.$userId.'/friends');

for($i=0;$i<=3;$i++){ //retrieved friends (max 4 persons)
        $value=$userFriends["data"][$i];
        echo "<div id='$value[id]'>";
        $profile = $facebook->api("/".$value["id"]);
        $friendsWork = $facebook->api("/".$value["id"]."?fields=work");
        echo "<strong>".$profile["name"]."<br></strong>";
        echo " <img src='https://graph.facebook.com/".$value["id"]."/picture'><br>";
        echo "Username : ".$profile["username"]."<br>";
        echo "Local : ".$profile["locale"]."<br>";
        echo "Birthdate : ".$profile["birthday"]."<br>";

        print_r($friendsWork); // <--- the result is an array

        echo "<hr>";
        echo "</div>";
    }
$userFriends=$facebook->api('/'.$userId./friends');
对于($i=0;$iapi(“/”$value[“id”]);
$friendsWork=$facebook->api(“/”$value[“id”]。“?fields=work”);
echo“”$profile[“name”]。“
”; 回声“
”; 回显“用户名:”.$profile[“用户名”]。“
”; echo“Local:”.$profile[“locale”]。“
”; 回音“生日:”.$profile[“生日”]。“
”;
print_r($friendsWork);//您只需解析获得的数组,如下所示-

$userFriends = $facebook->api('/'.$userId.'/friends?fields=work,name');

foreach($userFriends['data'] as $friend)
{
   $friend_name = $friend['name'];
   $emp_name=array();
   // list of all employers of this friend
   if(isset($friend['work']))
   {
       foreach($friend['work'] as $work)
       {
         array_push($emp_name, $work['employer']['name']);
       }
   } 
   else
   {
       $emp_name = "N.A.";
   }
}

给你,没什么特别的,我只是照我在评论中说的做了

$json = '{
 "id": "xxx",
 "friends": {
"data": [
  {
    "name": "Indiarto Priadi",
    "work": [
      {
        "employer": {
          "id": "111178415566505",
          "name": "Liputan 6 SCTV"
        }
      },
      {
        "employer": {
          "id": "107900732566334",
          "name": "SCTV"
        }
      }
    ],
    "id": "502163984"
  },
  {
    "name": "Agustin Kertawijaya",
    "work": [
      {
        "employer": {
          "id": "138215336225242",
          "name": "Trader Corporation (Canada)"
        },
        "location": {
          "id": "110941395597405",
          "name": "Toronto, Ontario"
        },
        "position": {
          "id": "168673399850791",
          "name": "Desktop operator <Pre press>"
        },
        "start_date": "2006-06-01",
        "end_date": "2008-06-01"
      },
      {
        "employer": {
          "id": "114464911939560",
          "name": "Merrill Corporation Canada"
        },
        "location": {
          "id": "110941395597405",
          "name": "Toronto, Ontario"
        },
        "position": {
          "id": "190075304347365",
          "name": "Financial Print Project Manager"
        },
        "start_date": "2006-06-01",
        "end_date": "2011-10-01"
      }
    ],
    "id": "511990261"
  }
],
"paging": {
  "next": "https://graph.facebook.com/1065251285/friends?limit=2&fields=name,work&offset=2&__after_id=511990261"
}}}'; // remember the last two closing curlys, you left them out of the code block in the OP.

  $obj = json_decode($json);

  foreach ($obj->friends->data as $friend) {
    echo '<h1>' . $friend->name . '</h1>';
    foreach ($friend->work as $job) {
      echo 'Employee: ' . $job->employer->name . '<br/>';
    }
    echo '<hr>';
  }

从json解码到对象或数组,然后获取您想要的任何信息。您能给我代码吗?我尝试了json解码,但只返回了错误。ThanksI尝试回显$emp_名称并显示错误:“注意:未定义的索引:在第95行(第2个foreach)的PATH\index.php中工作警告:为foreach()提供的参数无效”事实上,在95'行的PATH\index.php中,所有用户可能都没有facebook上的工作信息。我已经编辑了答案,请检查。仍然显示相同的错误。我仍然不明白为什么在第二次foreach时出现错误。您使用了确切的代码吗?您能告诉我确切的错误吗?我已经编辑了代码,请进一步检查
Indiarto Priadi

Employee: Liputan 6 SCTV
Employee: SCTV
--------------------------------------

Agustin Kertawijaya

Employee: Trader Corporation (Canada)
Employee: Merrill Corporation Canada
--------------------------------------