1

I have the following code:

    <?php if (!$user) { ?>
    <div class="fb-login-button" data-perms="email,user_birthday,publish_stream">Login with Facebook</div>
<?php } else { ?>

    Your user profile is
    <pre>
    <?php //print htmlspecialchars(print_r($user_profile, true)) ?>
    </pre>
    <?php echo $user_profile['name']; ?>

    <?php

    try{
        $data = array("message" => "Hello Woghfd!");
        $status = $facebook->api("/me/feed", "POST", $data); 
    }catch(FacebookApiException $e) {
        $login_url = $facebook->getLoginUrl( array(
                       'scope' => 'publish_stream'
                       )); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
    }


    ?>
<?php } 

I am using both JS and PHP SDK's... JS to log the users in, and PHP to get their info (experimental at the moment). My problem is that I have used the PHP to POST the data to the users FB feed, and it kept throwing an error if the user wasnt logged in.

I have solved the problem by using a try catch scenario to try and send the data to facebook, but if it fails, instead of throwing an error, it just asks the user to login with the PHP getLoginUrl finction.

Is this the best way to go about doing this? Or would you usually wrap this POST to feed functionallity in a onClick function or something?

Just asking because I think Im getting to grips with the whole thing and need to know if I am going in the right direction.

Thanks :)

1 Answer 1

2

The wireframe I usually use is based on two pages:

Index -> Page with login/start/go button. This button should trigger the JS SDK's login, catch the auth event, and redirect to the next page.

Main -> This page fetches the session that is already started by the index, if no session is available, it redirects to the index. It uses the session to do whatever the app needs to do with the facebook connection.

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

5 Comments

Ok thanks for the reply. I did consider this, but I would like it so the page doesnt need to re-direct? Like so its automatic.. can this way be automatic??
You mean do both things at the same time? Login and get the info?
No... it turns out ive found exactly what I want... basically I wanted it "AJAXified" so there are no reloading of pages or switching pages. I found a tutorial online which is perfect... follow this link please let me know what you think.
Also, am I right in saying... with the approach outlined in the link, the JS SDK sets up the auth, while the PHP SDK gets the access Token to validate the user, allowing the PHP API calls to work (as in my opinion it makes more sense to call API requests through PHP. Thanks
Yes, you are correct. The PHP SDK would "automagically" fetch the session that JS started. Sometimes you have session/cookie issues in IE, which would be fixed by a p3p header.

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.