如何解析facebookgraph对象并在视图中进行迭代


how to parse facebook graph object and iterate through in view?

我正在尝试解析facebook图形对象,并允许视图遍历对象及其数组。然而,我遇到了一个路障,视野一片空白。

facebook库:

require_once('Facebook/autoload.php');
use Facebook'FacebookSession;
use Facebook'FacebookRequest;
use Facebook'GraphUser;
use Facebook'FacebookRequestException;
use Facebook'FacebookRedirectLoginHelper;
class Facebook {
  protected $ci;
  public function __construct() {
    $this->ci =& get_instance();
    $this->ci->load->library('session');
    if(!isset($_SESSION)) {
      session_start();
    }
    FacebookSession::setDefaultApplication($this->ci->config->item('api_id', 'facebook'), $this->ci->config->item('app_secret', 'facebook'));
    $this->session = new FacebookSession($this->ci->config->item('app_token', 'facebook'));
  }
  public function get_user_profile($user) {
    $request = (new FacebookRequest($this->session, 'GET', '/' . $user))->execute()->getGraphObject(GraphUser::className());
    return $request;
  }
}

控制器:

$data['facebook'] = $this->facebook->get_user_profile("coach");
$this->load->template('default_template', $data);

视图:

foreach($facebook as $fb_item) {
  echo var_dump($fb_item);
  echo var_dump($facebook);
}

但我真正想做的是:

echo $fb_item->name; //to return name if its singular
echo $fb_item->cover->id; //for the multi dimensional objects
echo $fb_item->cover->source;

打印时返回的对象_r:

Facebook'GraphObject Object
(
    [backingData:protected] => Array
        (
            [id] => 24902886692
            [about] => The official page of Coach, a modern American luxury brand with a rich heritage of craftsmanship and New York style. Contact us at: http://bit.ly/1fdsZtA.
            [can_post] => 
            [category] => Retail and consumer merchandise
            [checkins] => 0
            [company_overview] => Coach is a modern American luxury brand with a rich heritage rooted in quality and craftsmanship. All over the world, the Coach name is synonymous with the ease and sophistication of New York style.
            [cover] => stdClass Object
                (
                    [cover_id] => 10152666534996693
                    [offset_x] => 0
                    [offset_y] => 0
                    [source] => https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-9/s720x720/10408485_10152666534996693_825399149096092916_n.jpg?oh=bb278cd2e136b99d5fb1a0823ff51f4d&oe=5591EAA7&__gda__=1431366774_f1edfc44f1a0e9ba2e83f491a21295ce
                    [id] => 10152666534996693
                )
            [description] => * * * * * * * * * *
Coach Facebook Fan Page Policy
We encourage open conversation on the Coach Facebook fan page. However, Coach reserves the right to delete any posts or images that we, in our sole discretion, deem inappropriate.
            [founded] => 1941
            [has_added_app] => 
            [is_community_page] => 
            [is_published] => 1
            [likes] => 5850158
            [link] => https://www.facebook.com/coach
            [location] => stdClass Object
                (
                    [city] => New York
                    [country] => United States
                    [state] => NY
                )
            [name] => Coach
            [parking] => stdClass Object
                (
                    [lot] => 0
                    [street] => 0
                    [valet] => 0
                )
            [talking_about_count] => 60665
            [username] => coach
            [website] => www.coach.com www.facebook.com/coach www.twitter.com/coach www.youtube.com/coach http://www.pinterest.com/coach/ http://instagram.com/Coach/
            [were_here_count] => 0
        )
)

关于如何进行,有什么建议或指导吗?

好的,我找到了解决方案。将对象作为数组返回:

getGraphObject()->AsArray();

然后您可以通过View进行迭代。