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

时间:2015-02-19 16:13:44

标签: php mysql json facebook-php-sdk user-data

我想使用php mysql将用户兴趣存储在数据库中。输出在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; }

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

1 个答案:

答案 0 :(得分:0)

解决了它。这是我用过的东西

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

对此的任何改进都将受到赞赏。感谢