2

我有这个脚本:这段代码应该发布一个文本和一个网站链接

<?
  // Remember to copy files from the SDK's src/ directory to a
  // directory in your application on the server, such as php-sdk/
  require_once('facebook.php');

  $c = array(
    'appId' => '4102137023*****',
    'secret' => '*****c4a60cb08*****7c0333*****',
  );

  $facebook = new Facebook($c);
  $uid = $facebook->getUser();
  echo "Userid: " . $uid;
  echo "<BR>";
?>
<html>
  <head></head>
  <body>

  <?
    if($uid){

      // We have a user ID, so probably a logged in user.
      // If not, we'll get an exception, which we handle below.
      try {
        $ret_obj = $facebook->api('/me/feed', 'POST',
                                    array(
                                      'link' => 'angel-craft.de',
                                      'message' => 'Wenn ihr das hier seht freut euch auf ein Game'
                                 ));
        echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';

      } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $login_url = $facebook->getLoginUrl( array(
                       'scope' => 'publish_stream'
                       )); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
        echo $e->getType();
        echo $e->getMessage();
      }   
      // Give the user a logout link 
      echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
    } else {

      // No user, so print a link for the user to login
      // To post to a user's wall, we need publish_stream permission
      // We'll use the current URL as the redirect_uri, so we don't
      // need to specify it here.
      $login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
      echo 'Please <a href="' . $login_url . '">login.</a>';

    }
  ?>

  </body>
</html>

这应该发布一个带有文本的链接,但 Userid($uid) 保持为空。

是的,这是来自 FB dev 的演示脚本。

4

2 回答 2

0

我已经成功使用了这个。

<?php 
require 'src/facebook.php';

    // Create our Application instance (replace this with your appId and secret).
    $facebook = new Facebook(array(
      'appId'  => '-----',
      'secret' => '-----',
      'cookie' => true,
    ));

    $session = $facebook->getSession();

    $me = null;
    // Session based API call.
    if ($session) {
      try {
        $uid = $facebook->getUser();
        $me = $facebook->api('/me');



        $Name = $me['first_name'];


        $properties = array(
                              array(
                                    'text'=>'Property Link', 
                                    'href'=>'http://www.yahoo.com'
                                    ),
                              array(
                                    'text'=>'Property Link', 
                                    'href'=>'http://www.yahoo.com'
                                    )
                              );

        // Link that is adjacent to "Like" and "Comment" at the very bottom of the post.
        $action_links = array(
                              'name'=>'Test', 
                              'link'=>'http://www.yahoo.com'
                              );
        // Dictates who can see the post.
        $privacy = array(
                         'value'=>'ALL_FRIENDS'
                         );

        // api('/me/feed', 'post',... = Wall Post.
        $wallPost = $facebook->api('me/feed', 'post', array(
                                                                 'message'=> 'Testing',                                     
                                                                 'link'=> 'http://www.yahoo.com',
                                                                                                                                 'properties'=>$properties,
                                                                 'actions'=>$action_links                                                           
                                                                 )
                                       );




      } catch (FacebookApiException $e) {

        error_log($e);
      }
    }

    $par = array();
    $par['req_perms'] = "email, publish_stream";


    if ($me) {
      $logoutUrl = $facebook->getLogoutUrl();
    } else {
      $loginUrl = $facebook->getLoginUrl($par);
    }
    ?>
于 2012-12-04T17:41:46.157 回答
0

我发现了这个错误,证书很旧。谢谢大家 :)

于 2012-12-04T17:58:43.147 回答