dovs36921 2015-02-16 18:23
浏览 49

如何解析facebook图形对象并在视图中迭代?

I am trying to parse the facebook graph object and allow the view to iterate through the objects and its array. However, I am hitting a roadblock and the view is showing blank.

facebook library:

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;
  }
}

controller:

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

view:

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

But what I really want to do is:

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;

the object that is returned when doing a print_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
        )

)

Any tips or direction on how to proceed?

  • 写回答

1条回答 默认 最新

  • douchongzhang9267 2015-02-17 00:54
    关注

    Ok I was able to figure out the solution. Return the object as an Array:

    getGraphObject()->AsArray();

    Then you can iterate through via the View.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题