php - Facebook api : iterate through posts -
i'm trying posts message , ids through loop, want posts' likes, i'm not there yet, 1 step @ time.
i'm getting page's name, url, picture, description, fans , posts. proceed in displaying them. posts apparently stdclass object, display print_r works.
facebooksession::setdefaultapplication('app','appsecret'); $session = facebooksession::newappsession(); $request = new facebookrequest( $session, 'get', '/myapp?fields=id,name,about,likes,link,,posts'); $response = $request->execute(); $content = $response->getgraphobject()->asarray(); ?> <h1><?= $content['name'] ?></h1> <p><img src="http://graph.facebook.com/<?= $content['id'] ?>/picture?width=180&height=180" /></p> <p>about: <?= $content['about'] ?></p> <p>fans: <?= $content['likes'] ?></p> <p>url: <?= $content['link'] ?></p> <p>posts: <?= print_r($content['posts']) ?></p>
so tried doing loop
foreach($content['posts'] $post){ echo $post['id']; echo $post['message'] }
however, indefined index errors id , message, , while why, i've not been able solve it.
print_r on posts outputs :
stdclass object ( [data] => array ( [0] => stdclass object ( [message] => postmessage. [created_time] => 2017-06-28t13:00:00+0000 [id] => a_post_id )
thanks help.
figured out myself, kaddath provided correct way :
foreach ($content['posts']->data $posts) { echo "<p>posts id: $posts->id</p>"; echo "<p>posts message: $posts->message</p>"; }
Comments
Post a Comment