如何在数据库中的 php sdk4 中存储 facebook 的 GraphObject?

How to store GraphObject of facebook in php sdk4 in database?

我想使用 php 在数据库中存储用户兴趣 mysql.The 输出在 JSON 中可用,我使用 print_r 打印的树状结构( ):

  Facebook/GraphUser Object
  (
      [backingData:protected] => Array
        (
           [data] => Array
              (
                [0] => stdClass Object
                 (
                   [category]=> Tours/sightseeing
                   [category_list] => Array
                    (
                      [0] => stdClass Object
                        (
                          [id] => 209888929023118
                          [name] => Landmark
                        )
                      [1] => stdClass Object
                        ......

我的代码:

  $user_interest = (new FacebookRequest($session, 'GET', '/me/interests'))  ->execute()->getGraphObject(GraphUser::className());

为了将数据添加到数据库中,我发现了以下代码,但它不起作用:

 for($i = 0; $i < sizeof($user_interest[data]); $i++) 
{
    $name = $user_interest[data][$i]['name'];
    $category = $user_interest[data][$i]['category'];
    $categoryId = $user_interest[data][$i]['id'];
    //$created_time = $user_interest[data][$i]['created_time'];

    $strsql = "INSERT INTO interests(categoryId,category,name) 
               VALUES($categoryId,$category,$name)";

    if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
    }
    else {
echo "Error: " . $sql . "<br>" . $conn->error; }

请帮助我真的被困在这里...

解决了。这是我用的

       $books = (new FacebookRequest($session, 'GET', '/me/books'))
        ->execute()->getGraphObject()->asArray();


  $booksarray=array();
    for($i=0;$i<10;$i++){
      $array = get_object_vars( $books['data'][$i]);
      $booksarray[]=$array['name'];
    }

然后我简单地将数组存储在 db:

 function interests($booksarray, $fbid){
    $query = "INSERT INTO Books      (uid,book1,book2,book3,book4,book5,book6,book7,book8,book9,book10) 
VALUES ('$fbid','$booksarray[0]','$booksarray[1]','$booksarray[2]','$booksarray[3]','$booksarray[4]','$booksarray[5]','$booksarray[6]','$booksarray[7]','$booksarray[8]','$booksarray[9]')";

 mysql_query($query); 
}

如有任何改进,我们将不胜感激。谢谢