?
Solved

PHP Facebook redirect popup with login

Posted on 2013-05-19
4
Medium Priority
?
3,574 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.
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 300 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 1200 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
The viewer will learn how to count occurrences of each item in an array.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

771 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