• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3728
  • Last Modified:

PHP Facebook redirect popup with login

Hey All,

I'm new to Facebook apps (and a little rusty in PHP as well).

I have a PHP page into which I would like to insert a link that - when clicked - generates a post for the user's Facebook wall.

The post will just be a link to an HTML page.

If the user is NOT logged in when the link is clicked, the popup window allows the user to login, and then continues to generate the post.

If possible, I would like the user to see the post in the popup window and need to click "post", just as if they were composing the post directly at Facebook.

Here is the code I have so far, but its not working.

Thanks in advance for the help!!

<?php

require_once("fb-php-sdk/facebook.php");
$config = array();
$config['appId'] = 'nnnn1285786nnnn';
$config['secret'] = 'nnnnnnnn12345nnnnnnnnnnnnn';
$config['fileUpload'] = false;  optional
$facebook = new Facebook($config);

function login_to_fb($facebook)
{
	$config['baseurl']  = 'http:www.mypage.net/';
 	$loginUrl = $facebook->getLoginUrl(array(
 			'display' => 'popup' ,
  	   		'next' => $config['baseurl'],
  	   		'redirect_uri' => $config['baseurl'],
     			'scope' => 'publish_stream',
  			'req_perms' => 'email'
 			)
 	);
}

function post_to_fb($facebook)
{
     $user_id = $facebook->getUser();
     if($user_id) {
       try {
         $ret_obj = $facebook->api('/me/feed', 
				   'POST',
                                   array('link' => 'http://www.mypage.com/shop/',
                                         'message' => 'Please buy stuff :-)!'
                                   ));
         echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
       } catch(FacebookApiException $e) {
 		login_to_fb($facebook);
       }   
     } else {
 		login_to_fb($facebook);
     }
}
?>

<html>
<body>

<script type="text/javascript">
function newPopup(url) {
    popupWindow = window.open(
		url,'popUpWindow','height=400,width=500,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
</script>
 
<div id='facebook'>
	<button onclick="newPopup('<?php post_to_fb($facebook); ?>')">fb</button> 
</div>

</body>
</html>

Open in new window

0
Mike R.
Asked:
Mike R.
  • 2
2 Solutions
 
Ray PaseurCommented:
I believe that this on line 7 will cause a PHP parse error to be reported on line 8.

$config['fileUpload'] = false;  optional
0
 
Mike R.Author Commented:
Ray: Thanks for catching that. It was supposed to be a comment. I've removed it, but the original error continues.

When I run the code, I get a button. When I press the button, I get a popup window...which is empty ("about:blank)".

I'm really not sure what the code is supposed to look like. I don;t need to keep any of this code...if there's something that works.

I AM restricted to PHP, Javascript and HTML tho.

Thanks!
0
 
mcnuteCommented:
Check your console while clicking the button which triggers the popup. This will show you any error within the javascript or network load error. The console is in IE pressing F12, in Firefox under Extras -> Developer and Chrome it is pressing Cmd+Alt+i, not sure how it is on windows systems.

Facebook has really good documentation about that in their developer central also. Check them out: https://developers.facebook.com/blog/
0
 
Mike R.Author Commented:
Thanks for all the advice. Here's what i came up with...

<?php
function post_to_facebook()
{
      require_once("lib/fb-php-sdk/facebook.php");
      
      $config = array();
      $facebook_text = "This is what I want to post on my wall!!";

      // Set Facebook app ID and secret
      $config['appId'] = '62xxxxxxxxxxxx6';
      $config['secret'] = 'd6xxxxxxxxxxxxxxxxxxxxxxxxxxxxd0';
      $config['fileUpload'] = false; // optional
      $facebook = new Facebook($config);
      
      $user = $facebook->getUser();

      if($user == 0) {
            $login_url = $facebook->getLoginUrl($params = array('scope' => "publish_stream"));
            echo ("<script> top.location.href='".$login_url."'</script>");
      } else {
            $token=$facebook->getAccessToken();
        try {    
              $post = $facebook->api("/me/feed","POST",array(
                  'message'       =>  $facebook_text,
                  'caption'       =>  "Teramyyd!",
                  'link'          =>  "http://www.ioworlds.com/shop",
                  'picture'       =>  "http://www.utr.net/ioworlds/images/teramyydMain.jpg"
              ));
              $url = 'http://www.facebook.com/'.$user;
              header("Location: $url" );
        }
         catch (FacebookApiException $e) {
            $result = $e->getResult();
           }
        }
}

post_to_facebook();
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now