简体   繁体   English

如何将Facebook URL图形API放入特定变量并使用php语言将其回显?

[英]How to put Facebook URL graph API into a specific variable and echo it out using php language?

So I was trying to put the value inside a variable, but I was having a hard time finding tutorials in google. 所以我试图将值放在变量中,但是我很难在google中找到教程。 Maybe someone could help me finding the solution. 也许有人可以帮助我找到解决方案。

  <?php
   require_once __DIR__ . '/graphSDK/src/Facebook/Facebook.php'; 
   require_once __DIR__ .'/graphSDK/src/Facebook/autoload.php';


   $fb = new Facebook\Facebook([
    'app_id' => '204902076724611',
    'app_secret' => 'a38fb1ef6db1caca2f5cbc5314defdfd',
    'default_graph_version' => 'v2.11'
  ]);

  try {
    $response = $fb->get('/764215210440736/feed?
    fields=comments{comments{message,from},message,from},message,from');
   $page = $response->getGraphEdge();
    print_r('<pre>'.$page.'<pre>');
  }

} catch(FacebookExceptionsFacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

What I really want is how do I fetch specific fields on the facebook for example the comment_id and message, I put them inside a variable and then echo them out. 我真正想要的是如何在Facebook上获取特定字段,例如comment_id和message,然后将它们放在变量中,然后回显它们。 Can anyone help me? 谁能帮我?

This is the result after using the code above 这是使用上面的代码后的结果

As writed in the comments you can do a foreach() loop to iterate over the JSON response array from graph API. 如注释中所写,您可以执行foreach()循环以从图API遍历JSON响应数组。

example: 例:

foreach($page->comments as $comment): $message = $comment['message']; $messageID = $comment['comment_id']; endforech;

Remember to respect the structure of the JSON response. 请记住要尊重JSON响应的结构。 If I remember well, on the graph api documentations there are some example on how to iterate over the api response. 如果我没记错的话,在图形api文档上,有一些有关如何遍历api响应的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM