从PHP发布链接

时间:2012-12-04 16:56:08

标签: php facebook-graph-api

我有这个脚本: 此代码应将文本和链接发布到网站

<?
  // 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的演示脚本。

2 个答案:

答案 0 :(得分: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);
    }
    ?>

答案 1 :(得分:0)

我发现了这个错误,证书很旧。感谢所有人:)