1

I am working with the Facebook SDK and PHP. Everything is working fine, but I am having trouble with one part, which is storing a specific field value into a variable.

Here is the code, which gets and prints the info.

if ($session){ //if we have the FB session

    $user_profile = (new FacebookRequest($session, 'GET', '/me/accounts?fields=id,name,access_token'))->execute()->getGraphObject(GraphUser::className());
    //do stuff below, save user info to database etc.
    echo "Session Set.";

    echo '<pre>';
    print_r($user_profile); //Or just print all user details
    $variable = print_r($user_profile, TRUE);
    echo '</pre>';

    // How do I get the first 'id', 'name', and 'access_token'
    // How do I get the second 'id', 'name', and 'access_token'
}

And here is what prints (I replaced the actual data with XXXX for obvious reasons):

Session Set.
Facebook\GraphUser Object
(
    [backingData:protected] => Array
        (
            [data] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => XXXXXXXX
                            [name] => XXXXXXXX
                            [access_token] => XXXXXXXXX
                        )

                    [1] => stdClass Object
                        (
                            [id] => XXXXXXXXX
                            [name] => XXXXXXXXX
                            [access_token] => XXXXXXXXX
                        )

                )

            [paging] => stdClass Object
                (
                    [cursors] => stdClass Object
                        (
                            [before] => XXXXXXX
                            [after] => XXXXXXX
                        )

                )

        )

)

How do I take [id], [name], and [access_token] and store them in variables so I can eventually save them into a db?

1 Answer 1

1

So if you're trying to integrate with the Facebook SDK using PHP, I highly recommend reading through Facebook's developer guide on it here:

https://developers.facebook.com/docs/php/gettingstarted

Here is a very helpful YouTube tutorial on using the Facebook SDK:

https://www.youtube.com/watch?v=P9ktjGNeAzg

Here is his GitHub files regarding the Facebook SDK:

https://github.com/sohaibilyas/facebook-php-sdk-v5

The file that wound up leading me to figure out how to get a Facebook Page ID and Access_Token was add-page-tab.php

Here is the piece of the code that will provide you with your info:

$pages = $fb->get('/me/accounts');
$pages = $pages->getGraphEdge()->asArray();
?>

<form action="XXXXXXXXXX" method="POST">
<select name="page" single>

<?php
foreach ($pages as $key) {
    ?>
    <option value="<?php echo $key['id']; ?>"><?php echo $key['name']; ?></option>
    <?php
}

?>
</select>
<input type="submit" name="submit">
</form>
<?php

if(isset($_POST['submit'])) {
    $page = $fb->get('/' . $_POST['page'] . '?fields=access_token, name, id');
    $page = $page->getGraphNode()->asArray();

    echo "Access Token: " .$page['access_token'] . "<br/><br/>";

    echo "Page Id: " . $page['id'];

    // print_r($page);

}

You can then easily store the variables into a DB, SESSION, COOKIE, etc...and carry out different functions.

Hope this helps someone. My best advice is go through the tutorials and read the Facebook guide in full. You'll save a lot of time and headache not trying to take a shortcut!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.