[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

PHP Facebook redirect popup with login

Posted on 2013-05-19
4
Medium Priority
?
3,649 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…
Suggested Courses

656 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