从Facebook API将用户数据保存到数据库

时间:2014-06-09 20:17:37

标签: php facebook api

我正在使用Facebook Users请求tagged_places FB API V-4

有了我正在做的数据

<?php  echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>'; ?>

当数据打印在屏幕上时,数据看起来很像

[data] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 111111111111111
                            [created_time] => 1905-01-01T08:00:00+0000
                            [place] => stdClass Object
                                (
                                    [id] => 1111111111111
                                    [location] => stdClass Object
                                        (
                                            [latitude] => 36.1313
                                            [longitude] => -95.9373
                                        )

                                    [name] => Tulsa, Oklahoma
                                )

                        )

                    [1] => stdClass Object
                        (
                            [id] => 11111111111
                            [created_time] => 2014-05-30T21:41:11+0000
                            [place] => stdClass Object
                                (
                                    [id] => 111111111111111
                                    [location] => stdClass Object
                                        (
                                            [city] => Okmulgee
                                            [country] => United States
                                            [latitude] => 35.623012460758
                                            [longitude] => -95.972782756346
                                            [state] => OK
                                            [street] => 104 S Morton Ave
                                            [zip] => 74447-5022
                                        )

                                    [name] => Ike's Downtown Pub & Eatery
                                )

                        )

如何将此信息保存到数据库中?

1 个答案:

答案 0 :(得分:2)

这是检索tagged_places

中每个值的方法
 $graphObject['tagged_places']->data->id
 $graphObject['tagged_places']->data->created_time
 $graphObject['tagged_places']->data->place->id
 $graphObject['tagged_places']->data->place->location->latitude
 $graphObject['tagged_places']->data->place->location->longitude
 $graphObject['tagged_places']->data->place->location->state
 $graphObject['tagged_places']->data->place->location->street
 $graphObject['tagged_places']->data->place->location->zip
 $graphObject['tagged_places']->data->place->name

这是将它们插入MySQL数据库的方法,

$stmt = $con->prepare('
    INSERT INTO this_should_be_your_table_name
    (id, created_time, place_id, latitude, longitude, state, street, zip, name)
    VALUES
    (?, ?, ?, ?, ?, ?)
');

foreach($graphObject['tagged_places']->data as $data) {
   $stmt->execute(array(
       $data->id,
       $data->created_time,
       $data->place->id,
       $data->place->location->latitude,
       $data->place->location->longitude,
       $data->place->location->state,
       $data->place->location->street,
       $data->place->location->zip,
       $data->place->name
   ));
}