Solved

PHP Facebook redirect popup with login

Posted on 2013-05-19
4
3,294 Views
Last Modified: 2013-05-27
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
Comment
Question by:Mike R.
  • 2
4 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 39178552
I believe that this on line 7 will cause a PHP parse error to be reported on line 8.

$config['fileUpload'] = false;  optional
0
 
LVL 3

Author Comment

by:Mike R.
ID: 39178587
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
 
LVL 11

Accepted Solution

by:
mcnute earned 400 total points
ID: 39180256
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
 
LVL 3

Author Comment

by:Mike R.
ID: 39198682
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now