Solved

PHP Facebook redirect popup with login

Posted on 2013-05-19
4
3,371 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 109

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will take you step-by-step through the basics of ad creation on facebook. You will learn how to create ads for mobile newsfeed, desktop newsfeed, and right column. 1. Login to your Facebook account. In the left column, click on “Crea…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
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…

828 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